按日期范围子集数据框

时间:2019-06-18 08:44:13

标签: python pandas

尝试以有效的方式按日期范围分隔数据框,到目前为止,我只想出了:

mask0 = df['Date of survey'].between('2010-01-01', '2010-12-31')
mask1 = df['Date of survey'].between('2011-01-01', '2011-12-31')
mask2 = df['Date of survey'].between('2012-01-01', '2012-12-31')

...
maskn = df['Date of survey'].between('nnnn-01-01', 'nnnn-12-31')

任何想法将不胜感激! (我将使用掩码对数据框进行子集化,并获取每年每个列变量的平均和。)

1 个答案:

答案 0 :(得分:1)

更好的方法是将DataFrame.resample使用meansum这样的聚合函数:

df1 = df.resample('A', on='Date of survey').agg(['mean','sum'])

或按DataFrame.groupby按年使用Series.dt.year

df2 = df.groupby(df['Date of survey'].dt.year).agg(['mean','sum'])