How To Merge Two Data Frame By Column Names Python
I have two data frame, one looks like this (shape: 12553*83): A12D A131 A12B ... 0 1.096131 2.609943 -0.659828 1 1.111370 2.650422 -0.648742 ..
Solution 1:
As @sushanth noted, use pd.concat()
-- with join='inner'
. Here is an example:
import pandas as pd
df1 = pd.DataFrame({'a': [1, 2, 3],
'b': [4, 5, 6],
'c': [7, 8, 9]})
df2 = pd.DataFrame({'b': [11, 12, 13],
'c': [14, 15, 16],
'd': [17, 18, 19]})
t = pd.concat([df1, df2], axis=0, join='inner')
print(t)
b c
0 4 7
1 5 8
2 6 9
0 11 14
1 12 15
2 13 16
More info here:
Solution 2:
Okay problems solved! Thanks to everyone's kind help! I first removed duplicate columns, then concated two tables. Like
df1 = df1.loc[:,~df1.columns.duplicated()]
merged = pd.concat([df1,df2],join='inner')
Post a Comment for "How To Merge Two Data Frame By Column Names Python"