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

codewizard at gmail.com codewizard at gmail.com
Wed Apr 11 16:30:24 EDT 2018


On Wednesday, April 11, 2018 at 2:49:01 PM UTC-4, zlju... at gmail.com wrote:
> 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]
> 
> Regards.

Try this:

df['C'] = df['B'].where(df['B'], other=df['A'])

Regards,
Igor.



More information about the Python-list mailing list