首页 > 解决方案 > 使用逻辑运算符创建新列

问题描述

我正在尝试创建一个新列,该列将分配“五或更多”、“减五或更少”、“五和减五之间”,并将另一列作为其输入:

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().

感谢任何帮助的人!

标签: pythonpandasapplyadd

解决方案


改成这个来修复你的 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)


推荐阅读