Reputation: 621
I have a dafarame like this:
df1:
col1 col2 data1 data2 data3
0 A A_1 2 4 5
1 A A_2 11 58 87
2 A A_3 14 24 54
3 B B_1 3 6 9
4 B B_2 1 38 77
5 B B_3 54 13 10
and i also have a dataframe lis this one:
df2:
col1 col2 sample1 sample2 sample3
0 A A_0 98 57 102
2 A A_1 6 13 5
2 A A_2 13 52 17
3 A A_3 8 29 50
4 B B_0 60 75 98
5 B B_1 3 6 9
6 B B_2 1 8 77
7 B B_3 2 1 10
So, how can I combine these dataframes, based on col1 and col2 and create a dataframe like this one:
col1 col2 sample1 sample2 sample3 data1 data2 data3
0 A A_0 98 57 102 NaN NaN NaN
2 A A_1 6 13 5 2 4 5
2 A A_2 13 52 17 11 58 87
3 A A_3 8 29 50 14 24 54
4 B B_0 60 75 98 NaN NaN NaN
5 B B_1 3 6 9 3 6 9
6 B B_2 1 8 77 1 38 77
7 B B_3 2 1 10 54 13 10
Upvotes: 0
Views: 731
Reputation: 29
If you want to base your conditions for both the values of 'col1' and 'col2', this minor adjustment would certainly help.
data = pd.merge(df1, df2, on=['col1',col2'],how='outer')
Upvotes: 0
Reputation: 1994
Use pandas.merge
The on
argument defines what column you want to merge the dataframes on and the
how
keyword defines what type of merge you want. Please look at the documentation to confirm what type of merge you want. But I think in this case you want the outer
merge.
print(pd.merge(df1, df2, on='col2',how='outer'))
Upvotes: 1