Pandas DataFrame 合并求和列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23361218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas DataFrame merge summing column
提问by Nilani Algiriyage
I'm trying to merge two DataFramessumming columns value.
我正在尝试合并两个DataFrames求和列的值。
>>> print(df1)
id name weight
0 1 A 0
1 2 B 10
2 3 C 10
>>> print(df2)
id name weight
0 2 B 15
1 3 C 10
I need to sum weightvalues during merging for similar values in the common column.
我需要weight在合并期间对公共列中的相似值求和。
merge = pd.merge(df1, df2, how='inner')
So the output will be something like following.
所以输出将类似于以下内容。
id name weight
1 2 B 25
2 3 C 20
采纳答案by waitingkuo
In [41]: pd.merge(df1, df2, on=['id', 'name']).set_index(['id', 'name']).sum(axis=1)
Out[41]:
id name
2 B 25
3 C 20
dtype: int64
回答by joris
If you set the common columns as the index, you can just sum the two dataframes, much simpler than merging:
如果将公共列设置为索引,则只需将两个数据帧相加即可,比合并简单得多:
In [30]: df1 = df1.set_index(['id', 'name'])
In [31]: df2 = df2.set_index(['id', 'name'])
In [32]: df1 + df2
Out[32]:
weight
id name
1 A NaN
2 B 25
3 C 20
回答by Jan Kislinger
This solution works also if you want to sum more than one column. Assume data frames
如果您想对多列求和,此解决方案也适用。假设数据帧
>>> df1
id name weight height
0 1 A 0 5
1 2 B 10 10
2 3 C 10 15
>>> df2
id name weight height
0 2 B 25 20
1 3 C 20 30
You can concatenate them and group by index columns.
您可以连接它们并按索引列分组。
>>> pd.concat([df1, df2]).groupby(['id', 'name']).sum().reset_index()
id name weight height
0 1 A 0 5
1 2 B 35 30
2 3 C 30 45

