Group pandas DataFrame by Week in Python (Example)

 

On this page, you’ll learn how to group a pandas DataFrame by week number in Python.

The article will contain the following:

So now the part you have been waiting for, the exemplifying Python code:

 

Example Data & Imported Libraries

We will need the datetime module and the pandas library for the implementation. So let’s import them.

import datetime                                             # importing datetime module
import pandas as pd                                         # importing pandas library

The following data will be used as a sample for this Python tutorial:

df = pd.DataFrame({"dates" : [datetime.date(2018, 10, 10),  # generating sample DataFrame
                            datetime.date(2020, 10, 12),
                            datetime.date(2020, 10, 16),
                            datetime.date(2020, 10, 22),
                            datetime.date(2020, 11, 19),
                            datetime.date(2020, 11, 23),
                            datetime.date(2020, 10, 15),
                            datetime.date(2020, 6, 19)]})

 

Example: Grouping by Week Number Using isocalendar() & groupby()

The syntax below demonstrates how to group the dates with respect to the week number. To achieve this, first, we need to create a new column that consists of the week numbers of dates.

To implement this, we can utilize the isocalendar() function, which converts the dates into the ISO (The International Organization for Standardization) date format and has a field containing the ISO week number.

df["Week"] = df["dates"].apply(
    lambda date: datetime.date.isocalendar(date)[1])
print(df)
#        dates  Week
#0  2018-10-10    41
#1  2020-10-12    42
#2  2020-10-16    42
#3  2020-10-22    43
#4  2020-11-19    47
#5  2020-11-23    48
#6  2020-10-15    42
#7  2020-06-19    25

Afterwards, we can use the groupby() function to group the DataFrame by the week column, and, for instance, we can employ the count() function to count the frequency per week number as shown below.

print(df.groupby(["Week"]).count())                         # grouping the rows by their week numbers
#      dates
#Week       
#25        1
#41        1
#42        3
#43        1
#47        1
#48        1

 

Video & Further Resources

I have recently published a video on my YouTube channel, which demonstrates the Python codes of this post. You can find the video instruction below.

 

The YouTube video will be added soon.

 

In addition to the video, you might have a look at the related articles that I have published on my homepage. I have published numerous tutorials on related topics such as counting and groups.

 

Summary: This tutorial has shown how to perform group a pandas DataFrame by the week number in Python. Please let me know in the comments section below if you have any additional questions.

 

Ömer Ekiz Python Programming & Informatics

This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get further information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top