0

I am facing a problem in applying multiple Elif condition over a large dataset. The sample data is as below:

Id = ['AM12','AM21','AM31','AM41','AM66','AM81','AM77','AM87','AM27','AM69']
Exec = ['Athreyu','Megan','','Omar','Michael','','Oliver','','Jesus','']
AD_Executive= ['','Fer','Virat','John','','John','','','pandya','John']
Ex_FiscId= ['John','Sonal','','Ram','','Anthony','','','Sriju','']
full_nm = ['pulari','','Burgers','Saheb','Bhavya','Borah','Dutta','Upinder','Ruhaan','Rochan']
df_ex = pd.DataFrame(list(zip(Id, full_nm,Exec,AD_Executive,Ex_FiscId)), 
           columns =['Id', 'full_nm','Exec','AD_Executive','Ex_FiscId'])

I want to create a new column for Final Name. The condition which I am applying is -

def final(df_ex):
   if df_ex['Ex_FiscId'] != np.NaN:
    return df_ex['Ex_FiscId']
  elif (df_ex['Ex_FiscId'] == np.NaN) & (df_ex['AD_Executive'] != np.NaN):
    return df_ex['AD_Executive']
  elif (df_ex['Ex_FiscId'] == np.NaN) & (df_ex['AD_Executive'] == np.NaN) & (df_ex['Exec'] !=np.NaN):
    return df['Exec']
  elif (df_ex['Ex_FiscId'] == np.NaN) & (df_ex['AD_Executive'] == np.NaN) & (df_ex['Exec'] ==np.NaN):
    return df_ex['full_nm']

df_ex['Final'] = df_ex.apply(final, axis = 1)

But it is not yielding the desired output. The code only seems to read the first if condition ignoring the other conditions.

I am also attaching the input and desired output table for reference-

Id full_nm Exec AD_Executive Ex_FiscId
AM12 pulari Athreyu John
AM21 Megan Fer Sonal
AM31 Burgers Virat
AM41 Saheb Omar John Ram
AM66 Bhavya Michael
AM81 Borah John Anthony
AM77 Dutta Oliver
AM87 Upinder
AM27 Ruhaan Jesus pandya Sriju
AM69 Rochan John

Desired Output -

Id full_nm Exec AD_Executive Ex_FiscId Final
AM12 pulari Athreyu John John
AM21 Megan Fer Sonal Sonal
AM31 Burgers Virat Virat
AM41 Saheb Omar John Ram Ram
AM66 Bhavya Michael Michael
AM81 Borah John Anthony Anthony
AM77 Dutta Oliver Oliver
AM87 Upinder Upinder
AM27 Ruhaan Jesus pandya Sriju Sriju
AM69 Rochan John John
  • Everything, including `np.NaN`, is `!= np.NaN`, so you'd never get past that first block. You should use `pd.isna()` to check if something is null. But really you should implement all of this logic with `np.select`: See https://stackoverflow.com/questions/18194404/create-column-with-elif-in-pandas – ALollz Mar 18 '21 at 16:29

2 Answers2

0

let's try this:

df['final'] = df_ex.apply(lambda x: x[x[::-1].notnull().idxmax()], axis=1)

Output:

    Id  full_nm     Exec    AD_Executive    Ex_FiscId   final
0   AM12    pulari  Athreyu     NaN            John     John
1   AM21    NaN     Megan       Fer            Sonal    Sonal
2   AM31    Burgers NaN         Virat          NaN      Virat
3   AM41    Saheb   Omar        John           Ram      Ram
4   AM66    Bhavya  Michael     NaN            NaN      Michael
5   AM81    Borah   NaN         John           Anthony  Anthony
6   AM77    Dutta   Oliver      NaN            NaN      Oliver
7   AM87    Upinder NaN         NaN            NaN      Upinder
8   AM27    Ruhaan  Jesus       pandya         Sriju    Sriju
9   AM69    Rochan  NaN         John           NaN      John
ashkangh
  • 1,594
  • 1
  • 6
  • 9
0
def final(df_ex):
if df_ex['Ex_FiscId'] !="":
    return df_ex['Ex_FiscId']
elif (df_ex['Ex_FiscId']=="") & (df_ex['AD_Executive'] !=""):
    return df_ex['AD_Executive']
elif (df_ex['Ex_FiscId']=="") & (df_ex['AD_Executive'] =="") & (df_ex['Exec'] !=""):
    return df_ex['Exec']
elif (df_ex['Ex_FiscId']=="") & (df_ex['AD_Executive'] =="") & (df_ex['Exec'] ==""):
    return df_ex['full_nm']


df_ex['Final'] = df_ex.apply(final, axis = 1)