Create Day of Week Column in pandas DataFrame in Python (Example)


In this article, I’ll illustrate how to create a days of week column in a pandas DataFrame in the Python programming language.

The table of contents is:

Let’s do this!


Example Data & Add-On Libraries

In the example, we will need the datetime module and the pandas library, so let’s start with loading them.

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

The following data is used as the example data for this tutorial:

df = pd.DataFrame({"dates" : [, 8, 10),  # generating sample DataFrame
                  , 10, 12),
                  , 2, 14),
                  , 3, 19),
                  , 5, 23)]})
print(df)                                                  # printing DataFrame
#        dates
#0  2018-08-10
#1  2020-10-12
#2  2015-02-14
#3  2012-03-19
#4  2018-05-23

Have a look at the previously shown output of the Python console. It shows that our example DataFrame is composed of a single column of datetime objects.


Example: Creating Weekday Column using strftime() and weekday()

The following Python syntax illustrates how to add columns for weekday names and numbers. Here we use DataFrame.apply() function to do the operations on the DataFrame. In this case, we will use it to apply the strftime() and weekday() functions.

The strftime() function is generally used for formatting purposes, in addition to that, it returns the name of the weekday which we will utilize in this example. The weekday() function is used to get the weekday number of a datetime object.

df["Weekday"] = df["dates"].apply(lambda x: datetime.datetime.strftime(x, '%A')) 
df["Day Number"] = df["dates"].apply(lambda x: str(datetime.datetime.weekday(x)))
print(df)                                                  # printing updated DataFrame
#        dates    Weekday Day Number
#0  2018-08-10     Friday          4
#1  2020-10-12     Monday          0
#2  2015-02-14   Saturday          5
#3  2012-03-19     Monday          0
#4  2018-05-23  Wednesday          2


Video & Further Resources

Do you want to know more about the adding a day of week column in pandas DataFrame? Then you might want to have a look at the following video instruction on my YouTube channel. I demonstrate the topics of this article in the video:


The YouTube video will be added soon.


Furthermore, you could have a look at the other articles that I have published on my website. Some posts can be found below:


You have learned in this article how to generate a days of week column in pandas DataFrame in the Python programming language. Please let me know in the comments, if you have further comments and/or 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.