gpt4 book ai didi

python - 如何在python中将两个数据帧一个带值另一个带 bool 值变成一个数据帧?

转载 作者:行者123 更新时间:2023-12-05 03:21:32 25 4
gpt4 key购买 nike

例如,我有两个数据框:

dataframe1 将是

            A     B     C     D     E
values1 0.25 0.33 0.12 0.22 0.08
values2 0.20 0.50 0.89 0.65 0.75

dataframe2 将是

              A     B     C      D     E
boolean1 True False True False True
boolean2 False False True True True

并想要一个数据框的结果:

      A  B     C     D     E
1 0.25 0 0.12 0 0.08
2 0 0 0.89 0.65 0.78

因此,如果它在 dataframe2 中为 True,则只需从 dataframe1 中获取值,如果它为 False,则替换为 0。我该怎么做?

最佳答案

你可以使用

df1 = df1.where(df2.values, 0)
# or
df1 = df1.mask(~df2.values, 0)
print(df1)

A B C D E
values1 0.25 0.0 0.12 0.00 0.08
values2 0.00 0.0 0.89 0.65 0.75

关于python - 如何在python中将两个数据帧一个带值另一个带 bool 值变成一个数据帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72947937/

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