Skip to content
Advertisement

Drop Non-equivalent Multiindex Rows in Pandas Dataframe

Goal

If sub-column min equals to sub-column max and if min and max sub-column do not equal to each other in any of the column (ao, his, cyp1a2s, cyp3a4s in this case), drop the row.

Example

JavaScript

Want

JavaScript

Attempt

JavaScript

Note

The actual dataframe has 50+ columns.

Advertisement

Answer

Use DataFrame.xs for DataFrame by second levels of MultiIndex, replace NaNs:

JavaScript

Or convert data to strings:

JavaScript

Compare Dataframes by DataFrame.eq and test if all Trues by DataFrame.all and last filter by boolean indexing:

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