cgarai
cgarai

Reputation: 37

Pandas multiiindex boolean slice

I'm so new to this I don't have the vocabulary to properly frame the question. Nor do I know how to include the output very well.

I'm trying to slice a multi indexed dataframe of COVID19 data. I want to select data from countries other than China. I know how to slice using a multiindex based on countries I want to see, I just don't know how to look at everything but a country or set of countries.

                     1/22/20   1/23/20   1/24/20...
Country   Province  
China      Hubei       28        28         28
Italy       NaN         0         0          0
...

Obviously the dataframe is much bigger. All I want to do is slice by excluding rather than explicitly including.

df.loc['China']

Gives me all rows with China. How do I slice to exclude? Below doesn't work, but it gives the idea:

df.loc[!='China']

Any hints?

Thanks!

Upvotes: 1

Views: 61

Answers (1)

jezrael
jezrael

Reputation: 862641

Use Index.get_level_values with filtering in boolean indexing:

df1 = df[df.index.get_level_values(0) != 'China']
print (df1)
                  1/22/20  1/23/20  1/24/20
Country Province                           
Italy   NaN             0        0        0

Upvotes: 1

Related Questions