Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop multi-indexed rows of a DataFrame based on 'AND' condition between levels

I want to be able to drop rows from a multi-indexed dataframe object using multiple level criteria (with a logical AND joining the criteria).

Consider the pandas dataframe object given by:

import pandas as pd
df = pd.DataFrame(data = [[1,'x'],[2,'x'],[1,'y'],[2,'y']],
                   index=pd.MultiIndex(levels=[['A','B'],['a','b']],
                                       labels=[[0,1,0,1],[0,1,1,0]],
                                       names=['idx0','idx1']))

print(df) outputs:

           0  1
idx0 idx1      
A    a     1  x
B    b     2  x
A    b     1  y
B    a     2  y

I wish to eliminate the row where 'idx0'=='A' and 'idx1'=='a', so the end result is:

           0  1
idx0 idx1      
B    b     2  x
     a     2  y
A    b     1  y

It seems to me as if this cannot be done with the df.drop() method. A 'roundabout' way which gives the correct result is to do:

df = pd.concat([df.drop(labels='A',level=0),df.drop(labels='a',level=1)])
df = df.drop_duplicates()

But I figure that there has to be a better way...

like image 823
Charlie Avatar asked Feb 01 '16 04:02

Charlie


1 Answers

To address your question regarding .drop() - just pass the MultiIndex labels as tuple:

df.drop(('A', 'a'))

           0  1
idx0 idx1      
B    b     2  x
A    b     1  y
B    a     2  y
like image 85
Stefan Avatar answered Sep 20 '22 01:09

Stefan