How to Find Day of the Week in Pandas


You can use the following methods to find the day of the week in pandas:

Method 1: Find Day of Week as Integer

df['date_column'].dt.weekday

Method 2: Find Day of Week as String Name

df['date_column'].dt.day_name()

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'date': pd.date_range(start='1/5/2022', freq='D', periods=10),
                   'sales': [6, 8, 9, 5, 4, 8, 8, 3, 5, 9]})

#view DataFrame
print(df)

        date  sales
0 2022-01-05      6
1 2022-01-06      8
2 2022-01-07      9
3 2022-01-08      5
4 2022-01-09      4
5 2022-01-10      8
6 2022-01-11      8
7 2022-01-12      3
8 2022-01-13      5
9 2022-01-14      9

Example 1: Find Day of Week as Integer

The following code shows how to add a new column to the DataFrame that shows the day of the week in the date column as an integer:

#add new column that displays day of week as integer
df['day_of_week'] = df['date'].dt.weekday

#view updated DataFrame
print(df)

        date  sales  day_of_week
0 2022-01-05      6            2
1 2022-01-06      8            3
2 2022-01-07      9            4
3 2022-01-08      5            5
4 2022-01-09      4            6
5 2022-01-10      8            0
6 2022-01-11      8            1
7 2022-01-12      3            2
8 2022-01-13      5            3
9 2022-01-14      9            4

The new day_of_week column displays the day of the week where:

  • 0: Monday
  • 1: Tuesday
  • 2: Wednesday
  • 3: Thursday
  • 4: Friday
  • 5: Saturday
  • 6: Sunday

Example 2: Find Day of Week as String Name

The following code shows how to add a new column to the DataFrame that shows the day of the week in the date column as an integer:

#add new column that displays day of week as string name
df['day_of_week'] = df['date'].dt.day_name()

#view updated DataFrame
print(df)

        date  sales day_of_week
0 2022-01-05      6   Wednesday
1 2022-01-06      8    Thursday
2 2022-01-07      9      Friday
3 2022-01-08      5    Saturday
4 2022-01-09      4      Sunday
5 2022-01-10      8      Monday
6 2022-01-11      8     Tuesday
7 2022-01-12      3   Wednesday
8 2022-01-13      5    Thursday
9 2022-01-14      9      Friday

The new day_of_week column displays the day of the week as a string name.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Group by Week in Pandas DataFrame
How to Group by Month in Pandas DataFrame

Leave a Reply

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