Python Pandas条件式仅执行第一个“ If”语句

时间:2018-07-09 18:49:37

标签: python pandas

我有一个数据集“ df”,如下所示:

a     b     c    e  f  g
1     Nan   Nan  2  3  4
Nan   1     Nan  2  3  4
Nan   Nan   1    2  3  4

我想在数据框“ df”中创建一个新列“ d”,如下所示:

   a     b     c    d  e  f  g 
   1     Nan   Nan  1  2  3  4
   Nan   1     Nan  1  2  3  4
   Nan   Nan   1    1  2  3  4

因此,基本上,对于三列具有的所有非NaN值,“ d”是列“ a”,“ b”和“ c”的合并。在“ a”,“ b”和“ c”之间,如果一列为非NaN,则其他列为NaN。我需要数据集中的列“ e”,“ f”和“ g”,但它们与构造列“ d”没有任何关系

我在下面创建了此函数:

def func(df):
    if df['a'] is not None:
        return df['a']
    if df['b'] is not None:
        return df['b']
    if df['c'] is not None:
        return df['c']

,然后使用concatenated_df['d']=concatenated_df.apply(func, axis=1)为数据帧的每一行应用条件语句。但是,我只能得到第一个成功执行的if语句。其余的将NaN值返回到新列'd'

我还尝试将功能修改为

def func(df):
    if df['a'] is not None:
        return df['a']
    elif df['b'] is not None:
        return df['b']
    elif df['c'] is not None:
        return df['c']

但它仍然只执行第一个条件语句

2 个答案:

答案 0 :(得分:2)

您可以使用 first_valid_index

df['d'] = df.apply(lambda x: x[x.first_valid_index()], axis=1)

     a    b    c  d
0    1  NaN  NaN  1
1  NaN    1  NaN  1
2  NaN  NaN    1  1

答案 1 :(得分:1)

重复的问题中未提及的另一个选项:

 Ctc.Mgr2racf IN (Select MgrFilterRacf from tblAvs1Scoring) --This returns the same 2 values but doesnt work, note works with only 1 value present
        AND (title LIKE '%IV%' or Title like '%III%' or title LIKE '%Cust Relat%')  --Going to apply same logic here once I have a solution