get a day of week as a number from a dattime object pandas in python

To get the day of the week as a number (where Monday is represented as 0 and Sunday as 6) from a datetime object in pandas, you can use the dt.weekday property. Here's an example:

main.py
import pandas as pd

# Create a datetime object
dt = pd.to_datetime('2021-09-25')

# Get the day of the week as a number
day_of_week = dt.weekday()

print(day_of_week)
168 chars
10 lines

Output:

main.py
5
2 chars
2 lines

In this example, the weekday() function returns the day of the week as a number, where Saturday is represented as 5.

Alternatively, you can use the dt.dayofweek property to achieve the same result.

related categories

gistlibby LogSnag