[英]How can I sum column values that corrispond to a specific value of another column in a pandas DataFrame?
I have a python pandas DataFrame with (item,feature,grade) 我有一个带有(item,feature,grade)的python pandas DataFrame
item feature grade
1 1 0.8
1 2 0.3
2 1 0.6
...
and I have to sum all grade values for each same item, so for example 而且我必须对每个相同项目的所有成绩值求和,例如
for item 1 sum of grade is 1.1
and I have to put all the sum in a new DataFrame with (item,sumGrade): 并且我必须将所有总和放入带有(item,sumGrade)的新DataFrame中:
item sumGrade
1 1.1
2 0.6
...
How can I do this without using groupby and apply function? 不使用groupby和apply函数怎么办? Because I need a good performance in computation.
因为我需要良好的计算性能。
Thank you 谢谢
You can groupby
on 'item' column and then call sum
on the 'grade' column, additionally call reset_index
to restore the 'item' column back: 您可以
groupby
在“项目”一栏,然后调用sum
对“品位”专栏,还叫reset_index
恢复“项”列回:
In [10]:
df.groupby(['item'])['grade'].sum().reset_index()
Out[10]:
item grade
0 1 1.1
1 2 0.6
Not sure why you don't want to group but you can also set the index to 'item' and sum
on the index level: 不确定为什么不希望分组,但也可以将索引设置为“ item”并在索引级别
sum
:
In [11]:
df.set_index('item')['grade'].sum(level=0)
Out[11]:
item
1 1.1
2 0.6
Name: grade, dtype: float64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.