running man running man - 7 days ago 5
Python Question

Pandas add a column and fill values by using list comprehension if else

I want to add a column name

A
in
df
, and fill the values. If column
a1
has value, fill df.A with df.a1, else fill df.A with df.a2.

tm = pd.DataFrame({'a1':['astr1',np.nan,'astr2',np.nan],'a2'[np.nan,np.nan,np.nan,'astr3']})
tm

a1 a2
0 str1 NaN
1 NaN NaN
2 str2 NaN
3 NaN str2


I want this.

a1 a2 A
0 str1 NaN str1
1 NaN NaN NaN
2 str2 NaN str2
3 NaN str2 str2

Answer

You can use numpy.where with mask created by isnull:

tm['A'] = np.where(tm.a1.isnull(), tm.a2, tm.a1)
print (tm)

      a1     a2      A
0  astr1    NaN  astr1
1    NaN    NaN    NaN
2  astr2    NaN  astr2
3    NaN  astr3  astr3

Another solutions with combine_first or fillna:

tm['A'] = tm.a1.combine_first(tm.a2)
print (tm)
      a1     a2      A
0  astr1    NaN  astr1
1    NaN    NaN    NaN
2  astr2    NaN  astr2
3    NaN  astr3  astr3

tm['A'] = tm.a1.fillna(tm.a2)
print (tm)
      a1     a2      A
0  astr1    NaN  astr1
1    NaN    NaN    NaN
2  astr2    NaN  astr2
3    NaN  astr3  astr3

And last solution with update:

tm['A'] = tm.a1
tm.A.update(tm.a2)
print (tm)
      a1     a2      A
0  astr1    NaN  astr1
1    NaN    NaN    NaN
2  astr2    NaN  astr2
3    NaN  astr3  astr3