月份显示的是1到12的数字,而不是月份的名称。

huangapple go评论69阅读模式
英文:

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到12的数字,而不是月份的名称。

答案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'		}

huangapple
  • 本文由 发表于 2023年6月18日 20:42:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定