I have two columns and I’m trying to make a new one depending if one has a null value or not. I have the following example and I´m trying to use the np.where() function but it doesn´t seems to work.
import pandas as pd
import numpy as np
# DF 1
a = pd.DataFrame([1,'nan',2],columns=['a1'])
# DF 2
b = pd.DataFrame(['hola','hola','hola'],columns=['b1'])
# New Column
b['b2'] = np.where(a['a1'].astype(str) != 'nan', b['b1'] + a['a1'].astype(str))
The result for the new column ‘b2’ should be:
hola1
hola
hola2
The np.where
function also doesn´t has like an else option so I don´t know how to include that. I appreciate the help!
>Solution :
You need to "synchronize" a
and b
dataframes to make pairwise comparisons (possibly with pd.concat
):
b['b2'] = pd.concat([a.replace({'nan': ''}), b], axis=1).apply(lambda x:x['b1'] + str(x['a1']), axis=1)
print(b)
b1 b2
0 hola hola1
1 hola hola
2 hola hola2