简体   繁体   English

合并和求和两个匹配python pandas的数据帧

[英]merge and sum two dataframes where columns match python pandas

I might be approaching this is the wrong way. 我可能正在接近这是错误的方式。 If I have two dataframes a and b: 如果我有两个数据帧a和b:

Dataframe A: 数据帧A:

a b c
1 2 4
1 6 5
1 8 7

and Dataframe B: 和Dataframe B:

a b c d
1 2 4 9
1 6 5 7
1 8 7 10

And I want to join them, but cruically sum the columns where the column name matchs, and keep the column names that don't match. 我想加入它们,但是暂时将列名匹配的列加起来,并保留不匹配的列名。 So the final joined table would look like: 所以最终的连接表看起来像:

a   b   c   d
2   4   8   9
2   12  10  7
2   16  14  10

Note: The indexs would always match (ie the same number of records in the same order) 注意:索引始终匹配(即相同顺序的记录数相同)

You could call add on the larger df passing in the other df and call fillna : 你可以把add在较大的DF传入其他DF和呼叫fillna

In [18]:
df1.add(df).fillna(df1)

Out[18]:
   a   b   c   d
0  2   4   8   9
1  2  12  10   7
2  2  16  14  10

Another way is to use combine_first : 另一种方法是使用combine_first

In [20]:
df1.add(df).combine_first(df1)

Out[20]:
   a   b   c   d
0  2   4   8   9
1  2  12  10   7
2  2  16  14  10

You can align the dataframe, fill NAN with zero and perform simple addition. 您可以对齐数据框,将NAN填充为零并执行简单的添加。

d = pd.DataFrame(data, columns=['a','b','c'])
d2 = pd.DataFrame(data2, columns=['a','b','c','d'])
d, d2 = d.align(d2, fill_value=0)
sum_df = d + d2

In [23]: d
Out[23]: 
   a  b  c  d
0  1  2  4  0
1  1  6  5  0
2  1  8  7  0

In [24]: d2
Out[24]: 
   a  b  c   d
0  1  2  4   9
1  1  6  5   7
2  1  8  7  10

In [25]:sum_df
Out[25]: 
   a   b   c   d
0  2   4   8   9
1  2  12  10   7
2  2  16  14  10

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

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