Skip to content
Advertisement

Filter Pandas MultiIndex over all First Levels Columns

Trying to find a way of efficiently filtering all entries under both top level columns based on a filter defined for only one of the top level columns. Best explained with the example below and desired output.

Example DataFrame

JavaScript

Create filter for multiindex dataframe

JavaScript

Desired output:

JavaScript

Advertisement

Answer

You can reshape for simplify solution by reshape for DataFrame by DataFrame.stack with filter by DataFrame.where:

JavaScript

Your solution is possible, but more complicated – there is reshaped mask for repalce missing values by back and forward filling missing values:

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