使用 pandas Grouper按十年开始对DataFrame进行分组 [英] Grouping DataFrame by start of decade using pandas Grouper
本文介绍了使用 pandas Grouper按十年开始对DataFrame进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个从1973年1月1日到2014年12月31日的每日观测数据.
I have a dataframe of daily observations from 01-01-1973 to 12-31-2014.
一直在使用Pandas Grouper,到目前为止,每种频率的运行情况都不错:我想按70、80、90等十年来对它们进行分组.
Have been using Pandas Grouper and everything has worked fine for each frequency until now: I want to group them by decade 70s, 80s, 90s, etc.
我试图这样做
import pandas as pd
df.groupby(pd.Grouper(freq = '10Y')).mean()
但是,这将它们分为73-83、83-93等
However, this groups them in 73-83, 83-93, etc.
推荐答案
您可以对年份进行一些算术运算以将其精确到最近的十年:
You can do a little arithmetic on the year to floor it to the nearest decade:
df.groupby(df.index.year // 10 * 10).mean()
这篇关于使用 pandas Grouper按十年开始对DataFrame进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文