pandas group by week
Probably you have date
column as a string.
In order to use it in a Grouper
with a frequency, start from converting this column to DateTime
:
df['date'] = pd.to_datetime(df['date'])
Then, as date
column is an "ordinary" data column (not the index), use key='date'
parameter and a frequency.
To sum up, below you have a working example:
import pandas as pdd = [['2018-08-19 19:08:19', 'pga', 'yes'], ['2018-08-19 19:09:27', 'pga', 'no'], ['2018-08-19 19:10:45', 'lry', 'no'], ['2018-09-07 19:12:31', 'lry', 'yes'], ['2018-09-19 19:13:07', 'pga', 'yes'], ['2018-10-22 19:13:20', 'lry', 'no']]df = pd.DataFrame(data=d, columns=['date', 'user', 'answer'])df['date'] = pd.to_datetime(df['date'])gr = df.groupby(pd.Grouper(key='date',freq='W'))for name, group in gr: print(' ', name) if len(group) > 0: print(group)
Note that the group key (name
) is the ending date of a week, so dates from group members are earlier or equal to the date printed above.
You can change it passing label='left'
parameter to Grouper
.