python - 使用逻辑运算符创建新列
问题描述
我正在尝试创建一个新列,该列将分配“五或更多”、“减五或更少”、“五和减五之间”,并将另一列作为其输入:
df['Change']
Out[196]:
0 -0.398010
1 -3.980227
2 1.475952
3 0.000000
4 -2.043446
31514 NaN
31515 NaN
31516 NaN
31517 NaN
31518 NaN
Name: Change, Length: 30811, dtype: float64
我试过了:
df['new_column'] = df.apply(lambda x: 'Five Or More' if (df['Change'] >= 5) else 'Between Five And Minus Five')
df['new_column'] = df.apply(lambda x: 'Minus Five Or Less' if (df['Change'] <= 5) else 'Between Five And Minus Five')
对于这两个我都收到此错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
感谢任何帮助的人!
解决方案
改成这个来修复你的 lambda 函数?
df['new_column'] = df['Change'].apply(lambda x: 'Five Or More' if (x >= 5) else 'Between Five And Minus Five')
在您的 lambda 函数中,如果您设置 lambda x,则需要使用 x,并且应用需要在列上 - 而不是 df 本身。查看更多信息https://datatofish.com/if-condition-in-pandas-dataframe/
另一种方法是根据您的条件创建一个函数并将此函数应用于您的数据框,如下所示:
def conditions(x):
if(x>=5):
return 'Five or More'
elif(x<=-5):
return 'Minus Five or Less'
else:
return 'Between Five And Minus Five'
df['new_column'] = df['Change'].apply(conditions)
推荐阅读
- wordpress - 我可以从我自己的 wordpress 主题的代码中添加指向我的图像的链接吗?
- python - 从 imblearn 导入 RUSBoostClassifier 的问题
- c++ - 扩展欧几里得定理 - 两个数 A 和 B 是否可以有一对以上的 x 和 y
- time - Anylogic:测量服务时间
- angular - 如何从角度材料复选框中获取值
- javascript - 如何访问这个对象?它不断返回一串未定义的
- nao-robot - Nao 机器人的声音问题
- sql - 等待还是现在?就是那个问题。PosgreSQL 与 ORACLE
- java - 构建从不匹配的“模式”的最佳方法是什么?
- docker - 通过 docker swarm 托管的应用程序连接被拒绝