Reputation: 4565
I would like to mask a Pandas dataframe based on booleans in another dataframe.
So, I've created a pandas DataFrame of 1
and 0
:
boolean_data = [(1, 0), (0, 1), (0, 0)]
df_criteria = DataFrame(data=boolean_data, index=['A','B','C'], columns=['x','y'])
x y A 1 0 B 0 1 C 0 0
I would like to use the above df_criteria
to mask values of a second dataframe df_result
, i.e., where df_criteria(i,j)=1
, df_result(i,j)=0
.
df_result = DataFrame(
data=[(10, 20), (30, 20), (10, 10)],
index=['A','B','C'], columns=['x','y'])
df_result
(before masking)
x y A 10 20 B 30 20 C 10 10
df_result
(after masking)
x y A 0 20 B 30 0 C 10 10
Upvotes: 2
Views: 74
Reputation: 164673
Using pd.DataFrame.iloc
and numpy.where
:
df.iloc[:] = np.where(df_criteria, 0, df)
print(df)
x y
A 0 20
B 30 0
C 10 10
Upvotes: 1
Reputation: 323266
IIUC, Using mask
df_result.mask(df_criteria==1,0)
Out[55]:
x y
A 0 20
B 30 0
C 10 10
Upvotes: 3