I have two pandas dataframes.
df1
col1 col2 col3 col4
A C0079731 s1 abc
A C0079731 s2 abc
df2
col1 col2 col3
A C0079731 s1
A C0079731 s2
AA C0079731 s3
I would like to compare col2 and if any 'col3' value is missing, then add to 'df1'. The expected output is:
df1
col1 col2 col3 col4
A C0079731 s1 abc
A C0079731 s2 abc
AA C0079731 s3 abc
I tried so far is merging the two dataframes but how can I get the above expected output.
df_2 = df1.merge(df2, left_on='col2', right_on = 'col2', how = 'inner')
CodePudding user response:
Concat, ffill and then drop duplicates if the table is as simple as you presented it here
pd.concat([df1, df2]).ffill().drop_duplicates()