Paul Cavacas
Paul Cavacas

Reputation: 4454

How to pick specific columns Pandas dataframe when checking against NaN

I have a pandas dataframe in Python that looks something like

       AccountID_x    AccountId  AmountCD_x  AmountDOC_x  AmountDoc_x  
1              NaN  4001001copa       52.53        52.53          NaN   
2              NaN  4001001copa       52.53        52.53          NaN   
3      4001001copa          NaN       52.53        52.53          NaN   
4              NaN  4001001copa       52.53        52.53          NaN   

This dataframe is the result of a merge command to merge 2 dataframes together. What I want to do now is create a new column that will add either AccountID_x or AccountId based on which on is not Nan, so in the above example rows 1, 2, 4 would have the value of AccountId in it and row 3 would have the value from AccountID_x in some new column.

Upvotes: 1

Views: 73

Answers (3)

Tarifazo
Tarifazo

Reputation: 4343

You can also propagate fillna using apply:

df2['newcolumn'] = df2[['AccountID_x','AccountId']].apply(lambda x: x.fillna(method='ffill')[-1], axis=1)

Or equivalently (in your case):

df2['newcolumn'] = df2[['AccountID_x','AccountId']].apply(lambda x: x.fillna(method='bfill')[0], axis=1)

Upvotes: 1

Lucas Hort
Lucas Hort

Reputation: 854

Try this:

df['new_column'] = df.apply(lambda x: x['AccountId'] if pd.isnull(x['AccountID_x']) else x['AccountID_x'], axis=1)

Upvotes: 0

Vaishali
Vaishali

Reputation: 38415

You can use combine_first to combine the two

df['new_col'] = df['AccountId'].combine_first(df['AccountID_x'])

df['new_col']

1    4001001copa
2    4001001copa
3    4001001copa
4    4001001copa

Upvotes: 2

Related Questions