Python Dataframe:使用If Then Else逻辑创建新列 - >“系列的真值是模棱两可的”

问题描述 投票:1回答:2

我使用以下代码创建一个新列,其值基于我的Python数据帧的另外两列中的值派生。

# Create a list to store the data
MSP = []

for row in df_EVENT5_18['FLT']:
    if df_EVENT5_18['FLT'].str.contains('1234') & df_EVENT5_18['AR'].str.contains('ABC1'):
        MSP.append(29)
    elif (df_EVENT5_18['FLT'].str.contains('1234')) & (df_EVENT5_18['AR'].str.contains('ABC2')):
        MSP.append(25)
    else:
        MSP.append('')

# Create a new column from the list 
df_EVENT5_18['MSP'] = MSP

当我运行上面的代码时,我收到以下错误:

ValueError:Series的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all()。

python pandas if-statement conditional ambiguous
2个回答
0
投票

任何时候你认为你需要一个熊猫循环,再次检查你的代码。一个线索是你有for row in df_EVENT5_18['FLT']:,但你从来没有使用row

Find indicies that match a string

在这种情况下,我们可以简单地使用布尔评估来获取我们想要设置的索引:

has_flt_1234 = df_EVENT5_18['FLT'].str.contains('1234')
want_29 = has_flt_1234 & df_EVENT5_18['AR'].str.contains('ABC1')
want_25 = has_flt_1234 & df_EVENT5_18['AR'].str.contains('ABC2')

Setting values using boolean series

然后根据需要设置适当的行:

df_EVENT5_18['MSP'][want_25] = '25'
df_EVENT5_18['MSP'][want_29] = '29'

Test Code:

import pandas as pd

df_EVENT5_18 = pd.DataFrame(dict(
    FLT=['1234', '1234', '1235'],
    AR=['ABC1', 'ABC2', 'ABC1']
))

print(df_EVENT5_18)

has_flt_1234 = df_EVENT5_18['FLT'].str.contains('1234')
want_29 = has_flt_1234 & df_EVENT5_18['AR'].str.contains('ABC1')
want_25 = has_flt_1234 & df_EVENT5_18['AR'].str.contains('ABC2')

# Create a new column from the list
df_EVENT5_18['MSP'] = ''
df_EVENT5_18['MSP'][want_25] = '25'
df_EVENT5_18['MSP'][want_29] = '29'

print(df_EVENT5_18)

Results:

     AR   FLT
0  ABC1  1234
1  ABC2  1234
2  ABC1  1235

     AR   FLT MSP
0  ABC1  1234  29
1  ABC2  1234  25
2  ABC1  1235    

0
投票

尝试这样的事情:

df[['new_col']] = df[['a','b'].apply(lambda (a,b) : pd.Series(your condition here),axis=1)
© www.soinside.com 2019 - 2024. All rights reserved.