Table of Contents
Pandas is a popular Python library used for data analysis and manipulation. One of its useful features is the ability to find the day of the week for a given date. This can be achieved by using the “dayofweek” method, which returns the day of the week as an integer, with Monday being represented as 0 and Sunday as 6. This method can be applied to a Pandas Series or DataFrame containing date values. Additionally, Pandas also allows for further customization by specifying the start of the week and the naming convention for the days of the week. By utilizing these built-in functions, users can easily and efficiently find the day of the week in their data sets, making it a valuable tool for data analysis.
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 4The 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 FridayThe new day_of_week column displays the day of the week as a string name.
Cite this article
stats writer (2024). How can I find the day of the week in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-find-the-day-of-the-week-in-pandas/
stats writer. "How can I find the day of the week in Pandas?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-find-the-day-of-the-week-in-pandas/.
stats writer. "How can I find the day of the week in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-find-the-day-of-the-week-in-pandas/.
stats writer (2024) 'How can I find the day of the week in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-find-the-day-of-the-week-in-pandas/.
[1] stats writer, "How can I find the day of the week in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I find the day of the week in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Comments are closed.