简体   繁体   English

在熊猫中合并具有相同索引的两个数据框

[英]Merging two dataframes with same index in pandas

I have two data frames. 我有两个数据框。 I need to join them so that those index which have same name in both data frames are joined into one and their values summed. 我需要加入它们,以便将在两个数据框中具有相同名称的那些索引合并为一个并对其值求和。 Those index which do not exist in the other data frame are created and their valued inserted. 创建那些在另一个数据框中不存在的索引,并插入其值。 See example below. 请参见下面的示例。

dataFrame1: dataFrame1:

index   col1 col2 col3
A         3    0    4
C         4    1    2
D         3    5    6
G         3    0    0

dataFrame2 dataFrame2

index   col1 col2 col3
A         1    1    3
B         4    4    1
C         1    3    0
E         0    2    1
F         1    3    2

I need the following results: 我需要以下结果:

index   col1 col2 col3
A         4    1    7
B         4    4    1
C         5    4    2
D         3    5    6
E         0    2    1
F         1    3    2
G         3    0    0

how can I do this is pandas? 我该怎么做熊猫? Note: no value should be treated as zero unless it is zero or NaN in both data frames. 注意:除非在两个数据帧中均为零或NaN,否则任何值均不应视为零。

I think you can use add with combine_first and casting to int by astype : 我认为您可以将addcombine_first一起使用,并通过astype add其转换为int

print df1
       col1  col2  col3
index                  
A         3     0     4
C         4     1     2
D         3     5     6
G         3     0     0

print df2
       col1  col2  col3
index                  
A         1     1     3
B         4     4     1
C         1     3     0
E         0     2     1
F         1     3     2

print df1.add(df2).combine_first(df1).combine_first(df2).astype(int)
       col1  col2  col3
index                  
A         4     1     7
B         4     4     1
C         5     4     2
D         3     5     6
E         0     2     1
F         1     3     2
G         3     0     0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM