0

I would like to merge two dataframes. Both have the same column names, but different numbers of rows.

The values from the smaller dataframe should then replace the values from the other dataframe

So far I tried using pd.merge

pd.merge(df1, df2, how='left', on='NodeID)

But I do not know how to tell the merge command to use the values from the right dataframe for the columnes 'X' and 'Y'.

df1 = pd.DataFrame(data={'NodeID': [1, 2, 3, 4, 5], 'X': [0, 0, 0, 0, 0], 'Y': [0, 0, 0, 0, 0]})
df2 = pd.DataFrame(data={'NodeID': [2, 4], 'X': [1, 1], 'Y': [1, 1]})

The result should then look like this:

df3 = pd.DataFrame(data={'NodeID': [1, 2, 3, 4, 5], 'X': [0, 1, 0, 1, 0], 'Y':[0, 1, 0, 1, 0]})
1
  • df2.set_index('NodeID').combine_first(df1.set_index('NodeID')).reset_index() Commented Aug 29, 2019 at 15:13

1 Answer 1

2

This is can be done with concat and drop_duplicates

pd.concat([df2,df1]).drop_duplicates('NodeID').sort_values('NodeID')
Out[763]: 
   NodeID  X  Y
0       1  0  0
0       2  1  1
2       3  0  0
1       4  1  1
4       5  0  0
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.