我想根据另一个数据帧中的 bool 值来屏蔽 Pandas 数据帧。
因此,我创建了 1
和 0
的 pandas DataFrame:
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
(屏蔽之前)
x y A 10 20 B 30 20 C 10 10
df_result
(屏蔽后)
x y A 0 20 B 30 0 C 10 10
最佳答案
IIUC,使用掩码
df_result.mask(df_criteria==1,0)
Out[55]:
x y
A 0 20
B 30 0
C 10 10
关于python - 根据第二个数据帧设置数据帧的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50859683/