I have two columns in a dataframe and I need to create a new one based on them. For example:
df = pd.DataFrame(data={'a':[1.0,1.0,2.0], 'b':[3.0,3.0,3.0]})
df.iloc[1,0]=np.nan
a b
0 1.0 3.0
1 NaN 3.0
2 2.0 3.0
I need to add a column c which takes value from a when it is not null and otherwise from b. like:
a b c
0 1.0 3.0 1.0
1 NaN 3.0 3.0
2 2.0 3.0 2.0
Here is what I have tried:
def dist(df):
if df['a']:
return df.a
else:
return df.b
df['c']=df.apply(dist,axis=1)
but the result is not what I expected. Can anyone suggest what I should do? Thx!