You can do this using the pandas apply function.
If your dataframe is,
df = pd.DataFrame({'first' : ['test', 'test2'], 'second' : [np.nan, 'test3']})
print(df)
first second
0 test NaN
1 test2 test3
You can then use the apply function to create the 'result' column based on the other two columns:
df['result'] = df.apply(lambda row: row['first'] if pd.isna(row['second'])
else row['second'],
axis=1)
The important part is axis=1 as that tells apply to use rows rather than columns.
If you'd prefer to not use a messy looking lambda function, the following code does the same thing:
def func(row):
if pd.isna(row['second']):
return row['first']
else:
return row['second']
df['result'] = df.apply(lambda row: func(row), axis=1)
Both of these produce the following dataframe:
first second result
0 test NaN test
1 test2 test3 test3