如何在python中基于indexandsum进行groupby
我的数据是如何在python中基于indexandsum进行groupby,python,pandas,Python,Pandas,我的数据是 Count 1 5 2 10 3 15 4 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 20 输出: Group Sum 0-10 30 11-20 20 在一个组中按索引10分组。IIUC,您希望按索引的函数分组。就这么做吧: resul = df.groupby((df.index-1)//10).sum() #
Count
1 5
2 10
3 15
4 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0
13 0
14 0
15 20
输出:
Group Sum
0-10 30
11-20 20
在一个组中按索引10分组。IIUC,您希望按索引的函数分组。就这么做吧:
resul = df.groupby((df.index-1)//10).sum()
# adjust the index from 0,1,... to 1-10,11-20,...
resul.index = [f'{10 * i + 1}-{10 * (i+1)}' for i in resul.index]
它给出:
Count
1-10 30
11-20 20
请将代码和文本分开。还要用适当的输出来阐述问题。