1

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'
0

1 Answer 1

1
  1. English is part of the row index, but df.English tries to access a column.
  2. 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.