0

I have two data frames, one with three columns and another with two columns. Two columns are common in both data frames:

enter image description here

I have to update the Marks column of df1 from df2 where the data is missing only and keep the existing value as same in the df1.

I have tried pd.merge but the result created a separate column which was not intended.

CreepyRaccoon
  • 826
  • 1
  • 9
  • 19
Ashish
  • 1
  • 2
  • https://stackoverflow.com/questions/38152389/coalesce-values-from-2-columns-into-a-single-column-in-a-pandas-dataframe – Jack Jan 05 '23 at 18:37
  • Sir, I have to update data only with null values i.e. hv to apply vlookup in the existing column (without creating a new column) only for the cells where value is absent. – Ashish Jan 06 '23 at 09:53

1 Answers1

0

Following worked for me:

df1['Mark'] = df1.Marks_x.combine_first(df1.Marks_y)   
df1['Marks_x'] = df1['Mark']    
df1 = df1.drop(['Marks_y', 'Mark'], axis=1)    
df1 = df1.rename(columns = {'Marks_x':'Marks'})
Ashish
  • 1
  • 2