I applied the .loc methodolgy discussed in Select rows in pandas MultiIndex DataFrame because I was recieving a KeyError:'class' even though 'class' exists as (what I thought was a column name) in my existing dataframe. Later finding out that 'class' was one of two indexes in a multiindex ('group' being the secondary index). While the .loc function allows me to select rows with 'First','Second','Third' I'm struggling to determine how to then apply an additional condition to exclude rows where the second index ('group') has blank rows.
Current dataframe looks like this:
class | group | Column1 |
---|---|---|
First | A | 123 |
First | ||
Second | B | 123 |
Third | C | 123 |
Forth | D | 123 |
Current code looks like this:
keep_rows = df.loc[['First','Second','Third']]
My original code looked like this (and was throwing the KeyError due to referenced names beind indexs and not column names)
keep_rows = df[(df['class'].isin(['First','Second','Third'])) & (df['group'].isna())]
Desired dataframe:
class | group | Column1 |
---|---|---|
First | A | 123 |
Second | B | 123 |
Third | C | 123 |