Reputation: 966
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.
Upvotes: 4
Views: 1345
Reputation: 210822
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
Upvotes: 4