Pandas, create new column if previous column(s) are not in [None, '', np.nan]

zljubisic at zljubisic at
Wed Apr 11 14:48:46 EDT 2018

I have a dataframe:

import pandas as pd
import numpy as np

df = pd.DataFrame( { 'A' : ['a', 'b', '', None, np.nan],
                     'B'  : [None, np.nan, 'a', 'b', '']})

      A     B
0     a  None
1     b   NaN
2           a
3  None     b
4   NaN      

I would like to create column C in the following way:
column C = column B if column B is not in [None, '', np.nan]
else column A

How to do that?

I tried:

df['C'] = df[['A', 'B']].apply(lambda x: x[1] if x[1] in [None, '', np.nan] else x[0])

but I got all np.nan's.

Where am I wrong?

I am expecting to get column C as ['a', 'b', 'a', 'b', NaN]


More information about the Python-list mailing list