Using .loc on just second index in multiindex

@sacul has the most idiomatic answer, but here are a few alternatives.

MultiIndex.get_level_values

df[df.index.get_level_values('name') == 'Ai']

           value
year name       
1921 Ai       90
1922 Ai        7

DataFrame.query

df.query('name == "Ai"')

           value
year name       
1921 Ai       90
1922 Ai        7

DataFrame.loc(axis=0) with pd.IndexSlice

Similar to @liliscent’s answer, but does not need the trailing : if you specify axis=0.

df.loc(axis=0)[pd.IndexSlice[:, 'Ai']]

           value
year name       
1921 Ai       90
1922 Ai        7

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)