I have a dataframe:
a b c
0 nan Y nan
1 23 N 3
2 nan N 2
3 44 Y nan
I wish to have this output:
You can try
df['d'] = np.where((df.b == 'N') & (pd.notnull(df.c)), df.a*df.c, np.where(pd.notnull(df.a), df.a, np.nan))
a b c d
0 NaN Y NaN NaN
1 23.0 N 3.0 69.0
2 NaN N 2.0 NaN
3 44.0 Y NaN 44.0
See the documentation for pandas notnull, in your current code, you just need to change series.notnull to pd.notnull(series) for it to work. Though np.where should be more efficient
def f4(row):
if row['a']==np.nan:
return np.nan
elif (row['b']=="N") & (pd.notnull(row.c)):
return row['a']*row['c']
else:
return row['a']
df['d']=df.apply(f4,axis=1)