[英]How to use pandas Grouper with 7d frequency and fill missing days with 0?
I have the following sample dataset 我有以下样本数据集
df = pd.DataFrame({
'names': ['joe', 'joe', 'joe'],
'dates': [dt.datetime(2019,6,1), dt.datetime(2019,6,5), dt.datetime(2019,7,1)],
'values': [5,2,13]
})
and I want to group by names
and by weeks or 7 days, which I can achieve with 我希望按
names
周或7天进行分组,这是我可以实现的
df_grouped = df.groupby(['names', pd.Grouper(key='dates', freq='7d')]).sum()
values
names dates
joe 2019-06-01 7
2019-06-29 13
But what I would be looking for is something like this, with all the explicit dates 但我要寻找的是这样的,具有明确的日期
values
names dates
joe 2019-06-01 7
2019-06-08 0
2019-06-15 0
2019-06-22 0
2019-06-29 13
And by doing df_grouped.index.levels[1]
I see that all those intermediate dates are actually in the index, so maybe that's something I can leverage. 通过执行
df_grouped.index.levels[1]
我发现所有这些中间日期实际上都在索引中,所以也许这是我可以利用的东西。
Any ideas on how to achieve this? 关于如何实现这一点的任何想法?
Thanks 谢谢
Use DataFrameGroupBy.resample
with DatetimeIndex
: 将
DataFrameGroupBy.resample
与DatetimeIndex
DataFrameGroupBy.resample
使用:
df_grouped = df.set_index('dates').groupby('names').resample('7D').sum()
print (df_grouped)
values
names dates
joe 2019-06-01 7
2019-06-08 0
2019-06-15 0
2019-06-22 0
2019-06-29 13
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.