簡體   English   中英

如何與 Grouper 分組,但我想使用最近的日期作為 freq 中的參考

[英]How group by with Grouper but i want use the most recent date as reference in freq

我想在pandas Grouper中使用最后一個日期作為參考,而不是像代碼那樣使用第一個日期。

這個想法是下一個。

我的 df 是

date       value    c1   c2

2019-03-06  500000.0  1   1

2019-03-16  500000.0  2   2

2019-04-06  300000.0  1    1

當我使用:

df.groupby(pd.Grouper(key='date',freq='30D')):
2019-03-06  1000000.0  3

2019-04-05  300000.0  1

但我想要

2019-03-06  500000.0  1

2019-04-05  800000.0  3

我覺得應該是31天

df.groupby(pd.Grouper(key='date',freq='31D',closed='right', label='right')).sum()
Out[325]: 
               value  c1  c2
date                        
2019-03-06  500000.0   1   1
2019-04-06  800000.0   3   3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM