I need to merge the below two dataframes to yield the below result.
Table_1
foo1 | foo2 | date | value1 | value2 |
---|---|---|---|---|
a | b | 4/20 | 6 | NaN |
a | b | 4/19 | NaN | 2 |
a | b | 4/18 | NaN | 1 |
Table_2
foo1 | foo2 | date | value3 |
---|---|---|---|
a | b | 4/20 | 2 |
a | b | 4/10 | 1 |
I'm able to yield the below result using this merge.
table_1 = table_1.merge(table_2,how='outer',left_on=['foo1','foo2','date'],right_on=['foo1','foo2','date'])
Resulting table
foo1 | foo2 | date | value1 | value2 | value3 |
---|---|---|---|---|---|
a | b | 4/20 | 6 | NaN | NaN |
a | b | 4/19 | NaN | 2 | NaN |
a | b | 4/18 | NaN | 1 | NaN |
a | b | 4/20 | NaN | NaN | 2 |
a | b | 4/10 | NaN | NaN | 1 |
I need any of the rows that match with those three columns to merge together.
Ideal result
foo1 | foo2 | date | value1 | value2 | value3 |
---|---|---|---|---|---|
a | b | 4/20 | 6 | NaN | 2 |
a | b | 4/19 | NaN | 2 | NaN |
a | b | 4/18 | NaN | 1 | NaN |
a | b | 4/10 | NaN | NaN | 1 |
How should I merge these two dataframes?
you could do this:
pd.merge(Table_1, Table_2, how="outer", on=['foo1','foo2','date'])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.