samedi 6 février 2021

How to apply if else conditions to assign values to new column using lambda

How to apply if else conditions to assign values to new column using lambda

def func(df1,df2,by,suffix):
    df1 = df1.assign(found_on=lambda x: 'Y')
    df2 = df2.assign(found_on=lambda x: 'Y')
    df1 = df1.merge(df2, how='outer', on=by, suffixes=suffix)
    col1= 'found_on' + suffix[0]
    col2='found_on' + suffix[1]
    print(col1)
    df1 = df1.assign(delta_info=lambda x:"on both" if (df1[col1].notna() and df1[col2].notna()) else (("on" + suffix[1]) if (df1[col1].isna() and df1[col2].notna()) else (("on" + suffix[0]) if (df1[col1].notna() and df1[col2].isna()) else "review")))
    print(df1)

Aucun commentaire:

Enregistrer un commentaire