本文介绍了如何开始在特定日期 pandas 时间组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个按日期时间"12M"分组的df.我希望将其按12月31日结束的12M分组.目前是每年的1月31日结束的分组.似乎必须有一种简单的方法来进行此操作,但我一直找不到能找到我所要求的文件.我尝试设置TimeGrouper('12M'),但它不会从年初开始计数,而是使用第一个日期索引作为起点
I have a df that I have grouped by datetime '12M'. I would like it to be grouped by 12M ending Dec. 31. Currently it is yearly grouping ending Jan. 31. It seems that there must be a simple way of doing this but I have been unable to find anything documenting one I am asking for. I've tried setting TimeGrouper('12M') but it won't start the counting from the beginning of the year, but rather is uses the first date index as a starting point
dfy = dfy.groupby([pd.TimeGrouper('12M'), 'fec_id', 'trans_typ', 'cmte_id'])['amount'].sum()
dfy.head()
#dfy.to_csv('out.csv')
date fec_id trans_typ cmte_id
2007-01-31 C00002600 24K C00000729 1000
C00002840 1000
C00004325 1000
C00005157 1000
C00009985 1000
Name: amount, dtype: int64
推荐答案
您可以使用 Grouper
与freq=A
:
偏移别名.
g = df.groupby([pd.Grouper(level='date', freq='A'), 'cand_id', 'trans_typ'])['amount'].sum()
print g
date cand_id trans_typ
2001-12-31 H2HI02110 24K 2500
2007-12-31 H8IL21021 24K -1000
S6TN00216 24K 2000
2008-12-31 H2PA11098 24K 1000
H4KS03105 24K 49664
H6KS01146 24K 2000
H6KS03183 24K 1000
H8KS02090 24K 1000
S6TN00216 24K 2500
2009-12-31 H0MO00019 24K 500
H8MO09153 24K 500
S0MO00183 24K 1000
S0NY00410 24K 0
S2KY00012 24K 2000
S6OH00163 24K -4000
S6TN00216 24K -2000
S6WY00068 24K -3500
这篇关于如何开始在特定日期 pandas 时间组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!