简体   繁体   English

如何使用索引合并熊猫中的多个数据框列?

[英]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.

相关问题 熊猫数据框如何合并列 - Pandas Data Frame how to merge columns 在pandas数据框中选择多列,列索引为序列号 - select multiple columns in pandas data frame with column index as sequential number 如何使用 Pandas 将多列合并为 1 个指标? - How to merge multiple columns into 1 indicator by using Pandas? 如何合并不同数据框的两列,如果找到匹配项,请使用 pandas 在新列中写入“True” - How to merge two columns of different data frame and if the match is found write "True" in a new column using pandas 如何在 Python Pandas 的一个数据框中使用几列进行合并? - How to make merge using a few columns in one Data Frame in Python Pandas? 如何将具有日期时间索引的 Pandas 数据框按日期分组,将属于日期的值拆分为多列? - How can a Pandas Data frame with datetime index be grouped by date in a way that values belonging to the date are split into multiple columns? 如何从多列构建索引并设置为列熊猫数据框? - How to build index from multiple columns and set to a column pandas data frame? Pandas-如何在包括索引在内的多个列上进行合并 - Pandas- how to do merge on multiple columns including index Pandas:合并数据框,但总结重叠列 - Pandas: merge data frame but summing overlapping columns 如何通过索引值对熊猫数据框的列进行排序 - How to sort the columns of a pandas data frame by the values of an index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM