Home > OS >  Merging two columns when column1 is NaN
Merging two columns when column1 is NaN

Time:11-02

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 

     

CodePudding user response:

You can do the following:

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

CodePudding user response:

Try with

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