首页 文章

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

提问于
浏览
1

我使用以下代码创建一个新列,其值基于我的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:系列的真值是不明确的 . 使用a.empty,a.bool(),a.item(),a.any()或a.all() .

2 回答

  • 0

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

    查找与字符串匹配的标记

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

    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')
    

    使用布尔系列设置值

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

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

    测试代码:

    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)
    

    结果:

    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)
    

相关问题