Home > Net >  AttributeError when accessing MultiIndex of pandas dataframe
AttributeError when accessing MultiIndex of pandas dataframe

Time:11-30

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:

  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
  • Related