[英]How to merge multiple data frame columns in pandas by using index?
I have six pandas data frames like the following.我有六个熊猫数据框,如下所示。 I want to join them by using the id column, which is the index.
我想通过使用 id 列(即索引)来加入它们。 In the following, I have provided an example with three data frames.
在下文中,我提供了一个包含三个数据框的示例。
df1 =
id cnt1
A000 10
A001 20
A002 30
A010 10
A050 55
...........................
A317 20
df2 =
id cnt2
A000 10
A010 20
...........................
A316 20
df3 =
id cnt3
A010 20
................................
A318 20
After joining, I need one data frame like the following.加入后,我需要一个如下所示的数据框。
all_df =
id cnt1 cnt2 cnt3
A000 10 10 0
A001 20 0 0
A002 30 0 0
A010 10 20 20
A050 55 0 0
............................................................................
A316 0 20 0
A317 20 0 0
A318 0 0 20
Please let me know how to do it.请让我知道该怎么做。 Thanks in advance.
提前致谢。
IIUC, you want pd.concat
on columns IIUC,您希望
pd.concat
在列上
all_df = pd.concat([df1, df2], axis=1).fillna(0)
print(all_df)
cnt1 cnt2
id
A000 10.0 10.0
A001 20.0 0.0
A002 30.0 0.0
A010 10.0 20.0
A050 55.0 0.0
A317 20.0 0.0
A316 0.0 20.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.