gpt4 book ai didi

python - Pandas - 将行中的值设置为自身乘以另一个值

转载 作者:太空宇宙 更新时间:2023-11-04 07:29:07 24 4
gpt4 key购买 nike

假设我有下表:

multiply   total   tax
Y 500 .1
Y 250 .5
N 300 .5

只有当multiply 的值为Y 时,我才想将total 设置为total * tax

最终输出:

multiply   total   tax
Y 50 .1
Y 125 .5
N 300 .5

最佳答案

使用np.where

df['total'] = np.where(df['multiply'] == 'Y', df['total'] * df['tax'], df['total'])
print(df)

multiply total tax
Y 50.0 0.1
Y 125.0 0.5
N 300.0 0.5

关于python - Pandas - 将行中的值设置为自身乘以另一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51660119/

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