I am writing a code that calculates the average returns of the day but the code I wrote shows the months as number from 1 to 12 instead of the name itself. Below is the code I have written and the output.
This is the code I have written and its output
start_date = '2023-01-01'
end_date = '2023-12-31'
dates_2023 = pd.date_range(start=start_date, end=end_date)
df1 = pd.DataFrame(index=range(1, 32), columns=dates_2023.month.unique())
for date in dates_2023:
month = date.month
day = date.day
df1.loc[day, month] = month_name[month]
df1 = df1.sort_index()
for date in dates_2023:
month = date.month
day = date.day
df1.loc[day, month] = date.strftime('%b')
df1 = df1.sort_index()
df_grouped = df.groupby([df.index.month, df.index.day]).mean()
for index, row in df_grouped.iterrows():
month, day = index
df1.loc[day, month] = row['Daily_returns']
df1
one solution could be defining a Dictionary in which the value is the number of the month and key would be the name of the month. And whenever you want you can access the key of months.
month = { '01':'January',
'02':'February',
'03':'March',
'04':'April',
'05':'May',
'06':'June',
'07':'July',
'08':'August',
'09':'September',
'10':'October',
'11':'November',
'12':'December' }
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments