Weekday_name is the inbuilt method for finding the day of the week in pandas python. Also dayofweek function in pandas is used for getting the day of the week in numbers. Lets see how to
- Get the Day of the week from date in English in pandas
- Get the day of the week in number (starting from Monday , Monday = 0 and Sunday =6)
Let’s see an example of each
First lets create the dataframe
import pandas as pd import numpy as np import datetime date1 = pd.Series(pd.date_range('2012-1-1 12:00:00', periods=7, freq='M')) df = pd.DataFrame(dict(date_given=date1)) print(df)
so the resultant dataframe will be
Get the Day of the week in English in pandas
Weekday_name function Gets the day of the week in English Starting from Monday till sunday. Which is shown in below code
df['day_of_week'] = df['date_given'].dt.weekday_name print(df)
so the resultant dataframe will be
Get the Day of the week in Numbers – pandas
dayofweek function Gets the day of the week in numbers Starting from Monday (Monday =0, Tuesday=1, Wednesday=2,Thursday =3, Friday=4 , Saturday =5, Sunday =6)
df['day_of_week_in_number'] = df['date_given'].dt.dayofweek print(df)
so the resultant dataframe will be