delalma
delalma

Reputation: 928

Select value based on level filtering of mutiindex

I could find some answer which should have work but strangely it did not. Any help would be appreciated.

I have the following dataframe:

vendor    currency    value 
2         CKE         3
          PWW         2
          LPS         1
5         PWO         4

On this df I try to take only the following desired output with the code:

vendor    currency    value 
2         CKE         3
          LPS         1

CODE:

fiat = ['CKE','LPS','ZZZ']
df = df.loc[(2, fiat)]

ERROR:

KeyError: "None of [Index(['CKE','LPS','ZZZ'], dtype='object')] are in the [columns]"

Upvotes: 2

Views: 39

Answers (1)

jezrael
jezrael

Reputation: 862711

You can add : for select all columns, without it pandas incorrectly parse it like second value of tuple are not existing columns names, so error is raised:

fiat = ['CKE','LPS','ZZZ']
df = df.loc[(2, fiat), :]

print (df)
                 value
vendor currency       
2      CKE           3
       LPS           1

Upvotes: 1

Related Questions