queste
queste

Reputation: 310

Subsetting a dataframe by date ranges

Trying to separate a dataframe by date ranges in an efficient way, so far I have only come up with:

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')

Any ideas would be greatly appreciated! (I'd be using the masks to subset the dataframe and get the mean sums of each column variable for each year).

Upvotes: 1

Views: 56

Answers (1)

jezrael
jezrael

Reputation: 862511

Better here is use DataFrame.resample by years with aggregate functions like mean and sum:

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

Or use DataFrame.groupby by years by Series.dt.year:

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

Upvotes: 1

Related Questions