Ad
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
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.
Any ideas on how to achieve this?
Thanks
Ad
Answer
Use DataFrameGroupBy.resample
with DatetimeIndex
:
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
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad