I have a python pandas DataFrame with (item,feature,grade)
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
1 1.1
2 0.6
...
How can I do this without using groupby and apply function? 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:
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:
In [11]:
df.set_index('item')['grade'].sum(level=0)
Out[11]:
item
1 1.1
2 0.6
Name: grade, dtype: float64
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.