I have a situation where I want to create a new column in a Pandas DataFrame and populate it according to conditions involving 2 other columns. In this example:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.array([['value1','value2'],['value',np.NaN],[np.NaN,np.NaN]]), columns=['col1','col2'])
I would like to create a new column, 'new col', which consists of 1) the value in 'col2' if it is not NaN else, 2) the value in 'col1' if it is not NaN else, 3) NaN
I am trying this function with .apply() but it is not returning the desired result
def singleval(row):
if row['col2'] != np.NaN:
val = row['col2']
elif row['col1'] != np.NaN:
val = row['col1']
else:
val = np.NaN
return val
df['new col'] = df.apply(singleval,axis=1)
i want the values in 'new col' to be ['value2', 'value', 'nan']