From my date column I want to create another one with the months. But I would like create a specific range. For exammple Jan
will be from 2020-01-03
to 2020-02-03
. Feb
will be from 20-02-03
to 20-03-03
and so on.
This is my df:
import pandas as pd
import datetime as dt
#Start of the calendar
start='2020-01-01'
#End of the calendar
end='2020-12-31'
#Create the calendar df
cal_df = pd.DataFrame({"Date": pd.date_range(start, end)})
#Extract the day
cal_df['Day'] = cal_df['Date'].dt.day
#Extract the day name
cal_df['Day_name'] =cal_df[['Date']].apply(lambda x: dt.datetime.strftime(x['Date'], '%A'), axis=1)
I tried to use the if statement but does not behave like I want.
start = '2020-01-03'
end_date = '2020-02-03'
if [(cal_df['Date'] > start) & (cal_df['Date'] <= end_date)]:
cal_df['Month'] = 'Jan'
else:
cal_df['Month'] = ' others'
So the above code puts Jan
everywhere even if Date
is les than the specified end_date
.
Can someone tell what I am doing wrong?