Bootuz
Bootuz

Reputation: 557

How to access a single row by index in multiindex dataframe?

I have a pandas dataframe like this, where native-country and salary are indices. And I'd like to know, is it possible to access a a single row by native-country, for instance by Canada?

enter image description here

Upvotes: 1

Views: 106

Answers (1)

Chris Adams
Chris Adams

Reputation: 18647

IIUC you can use DataFrame.xs (cross-section)

df.xs(key='Canada', level=0, drop_level=False)

Upvotes: 1

Related Questions