My goal is to join multiple tables and 3 tables can be joined like this as expected.
import pandas as pd
df1 = pd.DataFrame({'A':[10,20,30]}, index=[1,2,3])
df2 = pd.DataFrame({'B':[200]}, index=[2])
df3 = pd.DataFrame({'C':[300]}, index=[3])
df1.join(df2).join(df3)
A B C
1 10 NaN NaN
2 20 200.0 NaN
3 30 NaN 300.0
However, whenever I join another table, as above, colums of the table are attatched to the right side of the existing one. So, Is there a neat way to join like this simple form? (B and C columns are experessed in one column and the colume name does not matter)
A
1 10 NaN
2 20 200.0
3 30 300.0
CodePudding user response:
How about this ?
import pandas as pd
pd.concat([df1,df2, df3], axis=1).set_index('A').sum(axis=1)
A
10 0.0
20 200.0
30 300.0
dtype: float64
CodePudding user response:
Use concat
-
df1.join(pd.concat([df2, df3.rename(columns={'C': 'B'})]))
# A B
#1 10 NaN
#2 20 200.0
#3 30 300.0