gpt4 book ai didi

python - 比较 Pandas 中的两列 - 错误 : The truth value of a Series is ambiguous

转载 作者:太空宇宙 更新时间:2023-11-04 03:16:54 25 4
gpt4 key购买 nike

我有一个名为 m 的数据框,它包含三列 abc。我想将 b 列与 ac 进行比较,并将值放入 d 列。

a   b   c1   5   72   7   83   1   94   8   6

But when I try this in the following code, I got the error as

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

Code:

if m['b'] < m['a']:
m['d'] = m['a']
elif m['d'] > m['c']:
m['d'] = m['c']
else:
m['d'] = m['b']
m

有办法吗?

最佳答案

您可以使用双 numpy.where :

m['d'] = np.where(m['b'] < m['a'], m['a'], np.where(m['b'] > m['c'], m['c'], m['b']) )
print m
a b c d
0 1 5 7 5
1 2 7 8 7
2 3 1 9 3
3 4 8 6 6

关于python - 比较 Pandas 中的两列 - 错误 : The truth value of a Series is ambiguous,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35924588/

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