Katty_one
Katty_one

Reputation: 351

Merging two columns when column1 is NaN

I have df and I want to merge these 2 columns in just 1 column bringing the values of value2 to the column value1 when value1 is NaN

value1     value2
 100         NaN
 123         NaN
 NaN         133
 NaN         123
 90          NaN

How can I get this:

Expected Output:

Value1
 100         
 123         
 133         
 123         
 90 

     

Upvotes: 0

Views: 24

Answers (2)

BENY
BENY

Reputation: 323226

Try with

out = df.bfill(1).iloc[:,[0]]

Upvotes: 1

Bruno Mello
Bruno Mello

Reputation: 4618

You can do the following:

df['Value1'] = df['value1']
df.loc[df['value1'].isna(), 'Value1'] = df['value2'] 

Upvotes: 1

Related Questions