Pandas Group By 和 Sum 每 N 行
问题描述:
我有时间序列数据,我想分组并计算每 3 行的总和.似乎是一项简单的任务,但我无法弄清楚.感谢您的帮助.
I have time series data and I want to group by and calculate the sum every 3 rows. Seems like a straightforward task but I'm not able to figure it out. I would appreciate your help.
以下是数据:
df =
AE_NAME ANSWERED_CALL
DATE
2018-10-08 Alec Sochacki 4.0
2018-10-09 Alec Sochacki 4.0
2018-10-10 Alec Sochacki 7.0
2018-10-11 Alec Sochacki 5.0
2018-10-12 Alec Sochacki 3.0
2018-10-15 Alec Sochacki 4.0
2018-10-16 Alec Sochacki 3.0
2018-10-17 Alec Sochacki 8.0
2018-10-18 Alec Sochacki 5.0
2018-10-19 Alec Sochacki 7.0
Column Date
是一个索引列.
Column Date
is an index column.
我希望输出如下:
AE_NAME ANSWERED_CALL
DATE
2018-10-08 Alec Sochacki 0 # It's ok to omit the first row
2018-10-10 Alec Sochacki 15
2018-10-15 Alec Sochacki 12
2018-10-18 Alec Sochacki 16
2018-10-19 Alec Sochacki 7
非常感谢.
答
首先将您的索引提升为一个系列.然后将 groupby
+ agg
与字典一起使用:
First elevate your index to a series. Then use groupby
+ agg
with a dictionary:
df = df.reset_index()
d = {'DATE': 'last', 'AE_NAME': 'last', 'ANSWERED_CALL': 'sum'}
res = df.groupby(df.index // 3).agg(d)
print(res)
DATE AE_NAME ANSWERED_CALL
0 2018-10-10 AlecSochacki 15.0
1 2018-10-15 AlecSochacki 12.0
2 2018-10-18 AlecSochacki 16.0
3 2018-10-19 AlecSochacki 7.0
您对第一行的逻辑有些不清楚,因此您可能需要再进行一两次操作.
You've got some unclear logic for the first row, so you may need one or two more operations.