Source Code:
(back to article)
import pandas as pd # create a sample dataframe df = pd.DataFrame({'date': ['2022-01-01', '2022-02-01', '2022-03-01']}) # convert the date column to datetime df['date'] = pd.to_datetime(df['date']) # extract the month and year separately df['month'] = df['date'].dt.month df['year'] = df['date'].dt.year # display the result print(df)
Result:
Report an issue