Pandas IF测试创建新列

时间:2018-03-10 18:00:32

标签: python-3.x pandas

任何想法如何根据数据框中3列的测试创建列C?

到目前为止我已经

df.loc[df['Negative'] > df['Neutral'] and df['Negative'] > df['Positive'], 
'C'] = 'Bad'

这给了我

ValueError:系列的真值是不明确的

2 个答案:

答案 0 :(得分:1)

添加$user = "someuser" $pass = "YOUR LONG PASSWORD GUID FROM ABOVE" | convertTO-securestring $creds = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$pass New-SSHSession -ComputerName "myPi" -Credential $creds ,而()使用按位和 - and

&

如果需要df.loc[(df['Negative'] > df['Neutral']) & (df['Negative'] > df['Positive']), 'C'] = 'Bad' 使用numpy.where

if-else

答案 1 :(得分:0)

您可以使用

df.C.where((df['Negative'] > df['Neutral'])& (df['Negative'] > df['Positive']), 'Bad')

以及

的整个if else语句
l=[]

for _,x in df.iterrows():

    if x['Negative'] > x['Neutral'] and x['Negative'] > x['Positive']:
        l.append('Bad')
    else :
        l.append('')

df.C=l