df = pd.DataFrame({'salary': [2000,5000,7000, 3500, 8000],'rate':[2,4,6.5,7,5],'other':[4000,2500,4200, 5000,3000],
'name':['bob','sam','ram','jam','flu'], 'last_name' :['bob','gan','ram', np.nan, 'flu' ]})
I have a dataframe as df1 and I need to populate the new column with values based on below conditions:
If
'name'is equal to'last_name'then'salary'+'other'If
'last_name'isnullthen'salary'+'other'If
'name'is not equal to'last_name'then('rate' * 'other')+'salary'
I tried the below code but it is not giving the correct result:
if np.where(df["name"] == df["last_name"]) is True:
df['new_col'] = df['salary'] + df['other']
else:
df['new_col'] = (df['rate'] * df['other']) + df['salary']