简体   繁体   中英

boolean mask on pandas dataframe with multiindex

I have a dataframe with multiindex and multiple columns:

>>> df = pd.DataFrame([['A1','B1',2,1],['A1','B2',1,3],['A2','B1',2,1]], columns=['key1','key2','val1','val2'])
>>> df.set_index(['key1','key2'], inplace=True)
>>> df

Out[276]: 
           val1  val2
key1 key2            
A1   B1       2     1
     B2       1     3
A2   B1       2     1

I also have a boolean mask indexed by single level of mutliindex from above df:

>>> mask = pd.DataFrame([['A1',True,False],['A2',False,True]], columns=['key1','val1','val2'])
>>> mask.set_index(['key1'], inplace=True)
>>> mask

Out[277]: 
       val1   val2
key1              
A1     True  False
A2    False   True

Is there a simple way, how to apply boolean mask on df ? I am only able to apply mask with the same shape and (multi)index structure...

Desired output would be:

              val1    val2
key1 key2            
A1   B1       2.0     NaN
     B2       1.0     NaN
A2   B1       NaN     1.0

any clues? thanks.

Try to use DataFrame.where() method:

In [453]: df.where(mask)
Out[453]:
           val1  val2
key1 key2
A1   B1     2.0   NaN
     B2     1.0   NaN
A2   B1     NaN   1.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM