How to call the month from dataframe's date if the date format is DD/MM/YYYY
Currently, I'm using the df['month'] = pd.DatetimeIndex(df['Date']).month
to get the month number and covert it to month name. When review the output, only realize it is getting the day
as month.
Data:
Date format: DD/MM/YYYY
01/01/2022, 15/01/2022, 03/02/2022, 20/02/2022, 06/03/2022, 18/03/2022
So, how can I correct it?
Code:
month_labels = {1: 'Jan', 2: 'Feb', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August',9: 'Sept', 10: 'Oct', 11: 'Nov', 12: 'Dec'}
df['month'] = pd.DatetimeIndex(df['Settlement_Date']).month
x= df['month'].apply(lambda x: month_labels[x])
print(x)
Result:
Jan, Jan, Mar, Feb, Jun, Mar
The month is in the middle of the date instead of first of the date.
Expected result:
Jan, Jan, Feb, Feb, Mar, Mar