[英]How can I sum the values in one column based on the floor'd value of another column in pandas?
[英]How can I sum column values that corrispond to a specific value of another column in a pandas DataFrame?
我有一个带有(item,feature,grade)的python pandas DataFrame
item feature grade
1 1 0.8
1 2 0.3
2 1 0.6
...
而且我必须对每个相同项目的所有成绩值求和,例如
for item 1 sum of grade is 1.1
并且我必须将所有总和放入带有(item,sumGrade)的新DataFrame中:
item sumGrade
1 1.1
2 0.6
...
不使用groupby和apply函数怎么办? 因为我需要良好的计算性能。
谢谢
您可以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
不确定为什么不希望分组,但也可以将索引设置为“ 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.