gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:18:00 26 4
gpt4 key购买 nike

我想根据另一个数据框中的 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    yA   1    0B   0    1C   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    yA  10   20B  30   20C  10   10

df_result(屏蔽后)

   x    yA  0   20B  30   0C  10  10

最佳答案

IIUC,使用mask

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com