I have the following dataframe:
df1 = pd.DataFrame(np.random.randint(80, 120, size=(4,2)),
index=pd.MultiIndex.from_product([['English', 'Japanese'], ['like', 'dislike']]),
columns=['girl', 'boy']
)
print(df1)
How can I access just the English
rows? This does not work:
print(df1.English) # there I get error: AttributeError: 'DataFrame' object has no attribute 'English'
CodePudding user response:
English
is part of the row index, butdf.English
tries to access a column.- The row index is a MultiIndex, so it should be indexed as a tuple of
(language, dis/like)
.
To select the English
rows, use xs
to get the cross-section:
df1.xs('English')
# girl boy
# like 87 91
# dislike 95 89
Or in this case, loc
also works since the language is the first level of the MultiIndex:
df1.loc['English']
# girl boy
# like 87 91
# dislike 95 89