pandas 基于多个键之一对熊猫系列的值求和?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27927122/
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
Sum the values of a series in pandas based on one of multiple keys?
提问by echolocation
I'm working with pandas in python, and I have a pandas Series object, that I can't for the life of me figure out. it essentially looks like this:
我在 python 中使用Pandas,我有一个Pandas系列对象,我一生都无法弄清楚。它基本上是这样的:
>>>print(series_object)
key1 key2 key3
First class 19438 Error1:0 117
16431 Error2:0 80
1 Error3:0 70
Second class 28039 Error4:0 65
Third class 2063 Error5:0 28
19439 Error6:0 25
Fourth class 25975 Error7:0 11
Fifth class 23111 Error8:0 7
1243 Error9:665 4
Error9:581 3
27525 Error10:0 3
1243 Error9:748 2
1247 Error11:65 2
1243 Error9:852 2
1247 Error11:66 2
Error11:70 1
Error11:95 1
Error11:181 1
Error11:102 1
Error11:160 1
I want a way to sum the values of this object where key2 matches, so that it changes series_objectto be:
我想要一种方法来对 key2 匹配的这个对象的值求和,以便它更改series_object为:
>>>print(series_object)
key1 key2 key3
First class 19438 Error1:0 117
16431 Error2:0 80
1 Error3:0 70
Second class 28039 Error4:0 65
Third class 2063 Error5:0 28
19439 Error6:0 25
Fourth class 25975 Error7:0 11
Fifth class 23111 Error8:0 7
1243 Error9:665 11
27525 Error10:0 3
1247 Error11:65 9
I've tried a lot of different things, and in a normal array, this wouldn't be an issue for me, but the pandas series object is new and confusing me. Could anyone provide some help?
我尝试了很多不同的东西,在普通数组中,这对我来说不是问题,但是 pandas 系列对象是新的并且让我感到困惑。有人可以提供一些帮助吗?
回答by Alex
You can use groupby.
您可以使用 groupby。
http://pandas.pydata.org/pandas-docs/stable/groupby.html#groupby-with-multiindex
http://pandas.pydata.org/pandas-docs/stable/groupby.html#groupby-with-multiindex
In your case
在你的情况下
series_object.groupby(level='key2').sum()
Or if you want to keep 'key1' information as well
或者,如果您还想保留“key1”信息
series_object.groupby(level=['key1', 'key2']).sum()

