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 |