0

I've got two dataframes i would like to join, however, the dont contain exactly the same rows.

I've got this in the dataframe1

test1 1 test2 3 test5 4 test6 5 test7 6 

And this in dataframe2

test1 4 test3 5 test4 6 test5 3 test6 3 

What I would like to achieve is the following

 col1 col2 test1 1 4 test2 3 test3 5 test4 6 test5 4 3 test6 5 3 test7 6 

or

col1 col2 test1 1 4 test2 3 0 test3 0 5 test4 0 6 test5 4 3 test6 5 3 test7 6 0 

2 Answers 2

1

Use pd.merge with how='outer':

In [1539]: df1 Out[1539]: col val 0 test1 1 1 test2 3 2 test5 4 3 test6 5 4 test7 6 In [1540]: df2 Out[1540]: col val 0 test1 4 1 test3 5 2 test4 6 3 test5 3 4 test6 3 In [1541]: df1.merge(df2, on='col', how='outer') Out[1541]: col val_x val_y 0 test1 1.0 4.0 1 test2 3.0 NaN 2 test5 4.0 3.0 3 test6 5.0 3.0 4 test7 6.0 NaN 5 test3 NaN 5.0 6 test4 NaN 6.0 In [1542]: df1.merge(df2, on='col', how='outer').fillna(0) Out[1542]: col val_x val_y 0 test1 1.0 4.0 1 test2 3.0 0.0 2 test5 4.0 3.0 3 test6 5.0 3.0 4 test7 6.0 0.0 5 test3 0.0 5.0 6 test4 0.0 6.0 
Sign up to request clarification or add additional context in comments.

1 Comment

Amazing! this is what im looking for!
0

Use merge, which is inner join by default:

pd.merge(dataframe1, dataframe2, left_index=True, right_index=True) 

Or join, which is left join by default:

dataframe1.join(dataframe2) 

Or concat, which is outer join by default:

pd.concat([dataframe1, dataframe2], axis=1) 

2 Comments

output wasnt exactly what i was after.
It's is also merging your df as requested.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.