python - 根据第二个数据帧设置数据帧的值

标签 python pandas

我想根据另一个数据帧中的 bool 值来屏蔽 Pandas 数据帧。

因此,我创建了 10 的 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/

相关文章:

c# - 使用属性包装方法 C#

python - 使用 Tensorflow 泊松回归模型进行预测

pandas - DataFrameGroupBy 整个组的条件过滤器

python 生成器重复

java - 有效地在不同的虚拟机之间复制对象

python - 如何在 python 中仅绘制 12 小时格式的时间数据

python - 如何将列标题设置为 Pandas 数据框中的第一行?

python - 为什么我在生成 pdf 时在 reportlab 中收到 "memory error"

python - 如何使用以字符串形式给出的 Pandas 来计算事件的持续时间?

python - 如何格式化Python解释器文本?