[英]Concatenate column of every n-th row while calculating sum of all n rows
I have a very specific way I want to transform a pandas dataframe.我有一个非常具体的方法,我想转换一个 pandas dataframe。 It currently looks like this:
它目前看起来像这样:
CREATED_DATE COUNT
0 2018-10-01 27
1 2018-10-02 27
2 2018-10-03 34
3 2018-10-04 23
4 2018-10-05 23
...
What I want is to combine my data in to 7-day-chunks, which means I want to concatenate every first and the seventh's CREATED_DATE value, while summing up all seven COUNT values.我想要的是将我的数据组合成 7 天的块,这意味着我想连接第一个和第七个的 CREATED_DATE 值,同时总结所有七个 COUNT 值。 Something like this:
像这样的东西:
CREATED_DATE COUNT
0 2018-10-01 to 2018-10-07 163
1 2018-10-08 to 2018-10-14 183
2 2018-10-15 to 2018-10-21 301
3 2018-10-22 to 2018-10-28 142
4 2018-10-29 to 2018-11-05 95
...
I tried several approaches using transform, but somehow never end up with the result I want.我尝试了几种使用变换的方法,但不知何故从未得到我想要的结果。
You can use groupby().agg()
:您可以使用
groupby().agg()
:
(df.groupby(np.arange(len(df))//7)
.agg({'CREATED_DATE': lambda x: x.min() + ' to ' + x.max(),
'COUNT': 'sum'})
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.