我有以下数据帧:
date 01/2018 01/2019 02/2018 ... 10/2017 11/2017 12/2017
id_x id_y id_x id_y id_x ... id_y id_x id_y id_x id_y
department ...
0 NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN
1 149.0 NaN 112.0 4.0 103.0 ... NaN NaN NaN NaN NaN
799 NaN NaN NaN NaN NaN ... 3.0 80.0 NaN 79.0 2.0
最初的数据有日期和时间戳。在我的输出中,日期被转换并分组为月份和年份(然后旋转)
我现在的问题是,我需要聚合或报告的时间间隔是定制的(基于用户输入-它们不总是每月一次)。间隔可以是:
-每月
-每年(日历年)
-按周期(从特定日期开始的一年(12个月)
-半年/6个月
-每季度
所使用的最小间隔始终是个月(永远不会是天或次)。使用的最大间隔为12个月(但不总是日历年)。因此,我似乎想回到我目前的几个月,然后“卷起他们”到更长的时期需要
我有一个函数,它输出每个间隔所需的“开始”日期列表
def DefineIntervals(start_date, end_date, Interval = 0):
timeframes_mos = pd.date_range(start_date,end_date, freq='MS').strftime('%m/%Y').tolist()
timeframes_qtr = pd.date_range(start_date,end_date, freq='QS').strftime('%m/%Y').tolist()
timeframes_yrs = pd.date_range(start_date,end_date, freq='12MS').strftime('%m/%Y').tolist()
return timeframes_mos, timeframes_qtr, timeframes_yrs
我纠结于是用它们构造一个新的数据帧,然后尝试某种形式的合并,还是简单地传递一个变量以在所需的时间间隔内输出这个数据帧-无论哪种方式都应该让我得到所需的最终结果,即报告自定义时间间隔,其中一些可能是从今年3月到明年2月底的一年
似乎以下操作可以聚合所需的帧:
定时器
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Grouper.html
周期
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Period.html
剪切
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.cut.html
重新采样
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.resample.html
我被困在如何进行和不确定什么是最有效的功能是使用(如果以上任何一项)鉴于需要
目前没有回答
相关问题 更多 >
编程相关推荐