This will be our example data frame:
color size
name
rose red big
violet blue small
tulip red small
harebell blue small
We can create a mask based on the index values, just like on a column value.
rose_mask = df.index == 'rose'
df[rose_mask]
color size
name
rose red big
But doing this is almost the same as
df.loc['rose']
color red
size big
Name: rose, dtype: object
The important difference being, when .loc
only encounters one row in the index that matches, it will return a pd.Series
, if it encounters more rows that matches, it will return a pd.DataFrame
. This makes this method rather unstable.
This behavior can be controlled by giving the .loc
a list of a single entry. This will force it to return a data frame.
df.loc[['rose']]
color size
name
rose red big