gpt4 book ai didi

python - 难以创建 Pandas Dataframe 的列

转载 作者:行者123 更新时间:2023-11-28 21:14:45 25 4
gpt4 key购买 nike

我想基于现有列创建数据框的新列,但我希望它以数据框中的另一个现有列为条件。以下代码不起作用。有谁知道为什么吗?

if CV['keyword'] == 0:
CV['left out'] = (CV['Prediction Numerator'] - (CV['Rate'] *10000))/(CV['Prediction Denominator'] - 10000)
else:
CV['left out'] = (CV['Prediction Numerator'] - (CV['Rate'] *10000 * 10))/(CV['Prediction Denominator'] - (10000 * 10))

我收到以下错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\bwei\Downloads\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\pandas\core\generic.py", line 709, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

这是我的数据框前 ​​4 列的片段。

        Zip  keyword  Prediction Numerator  Prediction Denominator  
0 01001 0 7650546.693200 40002.558782
1 01001 0 7650546.693200 40002.558782
2 01001 0 7650546.693200 40002.558782
3 01001 0 7650546.693200 40002.558782
4 01002 0 157.951741 0.718621
5 01002 0 157.951741 0.718621
6 01005 0 3600150.148240 20000.671431
7 01005 0 3600150.148240 20000.671431
8 01007 0 6932235.816260 30000.936191
9 01007 0 6932235.816260 30000.936191
10 01007 0 6932235.816260 30000.936191

谢谢,本

最佳答案

这应该有效:

CV.loc[CV['keyword']==0,'left out']=expression1
CV.loc[CV['keyword']!=0,'left out']=expression2

关于python - 难以创建 Pandas Dataframe 的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31303399/

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