df1 = pd.DataFrame({'a':[1,2,3],'x':[4,5,6],'y':[7,8,9]}) df2 = pd.DataFrame({'b':[10,11,12],'x':[13,14,15],'y':[16,17,18]})
I'm trying to merge the two data frames using the keys from the df1
. I think I should use pd.merge
for this, but I how can I tell pandas to place the values in the b
column of df2
in the a
column of df1
. This is the output I'm trying to achieve:
a x y 0 1 4 7 1 2 5 8 2 3 6 9 3 10 13 16 4 11 14 17 5 12 15 18
Different column names are specified for merges in Pandas using the “left_on” and “right_on” parameters, instead of using only the “on” parameter. Merging dataframes with different names for the joining variable is achieved using the left_on and right_on arguments to the pandas merge function.
The concat() function in pandas is used to append either columns or rows from one DataFrame to another. The concat() function does all the heavy lifting of performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on the other axes.
The concat() function can be used to concatenate two Dataframes by adding the rows of one to the other. The merge() function is equivalent to the SQL JOIN clause. 'left', 'right' and 'inner' joins are all possible.
Just use concat
and rename
the column for df2
so it aligns:
In [92]: pd.concat([df1,df2.rename(columns={'b':'a'})], ignore_index=True) Out[92]: a x y 0 1 4 7 1 2 5 8 2 3 6 9 3 10 13 16 4 11 14 17 5 12 15 18
similarly you can use merge
but you'd need to rename the column as above:
In [103]: df1.merge(df2.rename(columns={'b':'a'}),how='outer') Out[103]: a x y 0 1 4 7 1 2 5 8 2 3 6 9 3 10 13 16 4 11 14 17 5 12 15 18
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With