1

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
Corralien
  • 109,409
  • 8
  • 28
  • 52
2020db9
  • 153
  • 1
  • 9

1 Answers1

3

You can use get_level_values:

>>> df[(df.index.get_level_values('class').isin(['First','Second','Third'])) 
       & (df.index.get_level_values('group') != '')]

              Column1
class  group         
First  A        123.0
Second B        123.0
Third  C        123.0

Details:

>>> df.index.get_level_values('class').isin(['First','Second','Third'])
array([ True,  True,  True,  True, False])

>>> df.index.get_level_values('group').notna()
array([ True, False,  True,  True,  True])
Corralien
  • 109,409
  • 8
  • 28
  • 52