Reputation: 821
I have a df that has the following, df.dtypes
:
key object
date datetime64[ns]
username object
answer object
grade object
dtype: object
I then group by week:
test_lastweek = df.groupby([pd.Grouper(key='date', freq='W-SAT')])['key'].count()
I can see that that are 45 records that fall with in the last week of 2019-08-17
:
date
2019-06-29 475
2019-07-06 294
2019-07-13 2311
2019-07-20 389
2019-07-27 554
2019-08-03 408
2019-08-10 587
2019-08-17 45
Freq: W-SAT, Name: key, dtype: int64
Question: How do I get the last weeks data, all 45 records with the data from df
and make that into a new df?
Upvotes: 0
Views: 146
Reputation: 15
I think you can filter the df by the date column as
# Simple dates to datetimes of from datetime import datetime
df['Date'] = pd.to_datetime(df['Date'])
# Extract the week from the datetime column
df['Week'] = df['Date'].datetime.isocalendar().week
With this code, you can get the week of all your dates and then you'll have to filter it by the week you. Week values are int
type so you can do math operations with it, so you can subtract -1
to get a the previous week of the one selected.
max_sem = df['Week'].max()
last_sem = max_sem-1
The example is one I used to get the current week for some data and its predecessor.
Upvotes: 0
Reputation: 413
you can just filter the dataframe using .loc if you want all records from last week
df_last_week = df.loc[df['date'] >= '2019-08-17']
let me know if this helps.
Upvotes: 1