Skip to content
Advertisement

How do I filter multi-level columns using notnull() in pandas?

I generate a multi-index dataframe that has some NAN values using this:

JavaScript

Which will create something like this:

enter image description here

I’d like to get rows of a specific subset of top-level columns (eg df[['baz','qux']]) that have no nulls. For example in df[['baz','qux']] I’d like to get rows 0 and 1 since they both have all nulls in 3.

Hoping things would just work like a normal df I tried:

JavaScript

But I obviously am missing something:

JavaScript

The pandas documentation for multiindex/advanced indexing illustrates how to index and slic this sort of dataframe but doesn’t seem to have anything regarding .loc/lookups/filtering. So I assume I’m looking in the wrong place. But I am having trouble finding results or resources on this.

Advertisement

Answer

df[cols].notna() is not a 1D boolean mask. You have to reduce the dimension using all or any on axis.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement