Selecting#
Pandas
includes a few useful commands for isolating components of our `DataFrame.
Columns#
We can select columns using their index labels or name attribute.
df['MAR'] # returns MAR column
df.mar # returns MAR column
df[['MAR', 'AGEP']] # select multiple columns
When selecting multiple columns, the inner brackets ([]
) define the column names to subset or select. The outer brackets select data from a dataframe. In this multi-column example, age_sex
is a DataFrame
because it is a two-dimensional object.
Rows#
We can select rows using their index.
.loc
for location attribute.iloc
for numerical index
For our sample dataframe, the row indeces are integers, so we could use these commands interchangeably.
df.loc[2] # select third row
df.iloc[2] # select third row
Additional Resources#
Consult the “Different choices for indexing” documentation for more on indexing options.