英文:
Months are showing numbers 1 to 12 instead of the name of the month
问题
这是我翻译好的代码部分:
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
图片链接已提供。
英文:
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
答案1
得分: 2
一个解决方案可以是定义一个字典,其中值是月份的数字,键是月份的名称。每当需要时,您可以访问月份的键。
month = {
'01': '一月',
'02': '二月',
'03': '三月',
'04': '四月',
'05': '五月',
'06': '六月',
'07': '七月',
'08': '八月',
'09': '九月',
'10': '十月',
'11': '十一月',
'12': '十二月'
}
英文:
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' }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论