So I have got this Pandas DataFrame with multilevel index for the columns:
group1 group2 group3
1 2 1 2 1 2
0 ... ... NaN ... ... ...
1 NaN ... ... ... ... ...
2 ... ... ... ... NaN ...
Now i want to drop the rows where the columns group2
and group3
have NaN values. Which equates to rows 0 and 2 in this instance.
According to my understanding of the documentation this should work:
df.dropna(axis = 'rows', subset = ['group2', 'group3'])
But it does not. Instead I get the error:
KeyError: ['group2', 'group3']
Could someone please point out to me how to properly specify the subset?
Kind regards, Rasmus
Update
So it seems like .dropna() cannot work with mulitlevel column indexes. In the end I went with the less elegant, but workable method suggested, slightly rewritten:
mask_nan = df[['group2', 'group3']].isna().any(axis = 'columns')
df[~mask_nan] # ~ to negate / flip the boolean values