0

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')

1 Answer 1

2

Concat, ffill and then drop duplicates if the table is as simple as you presented it here

pd.concat([df1, df2]).ffill().drop_duplicates()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.