Months are showing numbers 1 to 12 instead of the name of the month

muhammad fairuzi

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

Ati Srv

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.

edited at
0

Comments

0 comments
Login to comment

Related

Dynamically Populating months as Month1, month2 etc instead of month name

Calculate average number of distinct ID's in a period(1 month,3 months ,6 months, 9 months and 12 months)

(Teradata) Get a SUM value of previous 12 months, 3 months and 1 month

Generate month end dates for last 12 months

Count of A Column For Last 12 Months and Individual Month

Getting Start & End of Month for 12 Months (A Year)

Calculate the average of the last 12 months for every month?

12 months dynamic array based on current month

How to compute the sum of orders over a 12 months period sliding by 1 month per customer in Spark

Get the Data for Each Month (till that month) for 12 months

Display month as numbers instead of words

Get slider to display months instead of numbers

How can I get the last 12 months from the current date PLUS extra days till 1st of the last month retrieved

Returning the month index/number instead of month name

Downsampling from 1 month to multiple months in Pandas

Month name from Month Numbers in android

Scala Get List of last 12 months from given month

Rolling sum previous 12 months per month (SQL- Snowflake)

Get the last 12 months in PHP from Month Year Given

How to Display Last rolling 12 months in correct month order

Delphi & SQLite: Get the records count per month for the last 12 months

MySQL get quantities from last 12 months grouped by month

PHP array containing 12 months and start with current month and decrement

Shiny showing numbers instead of dates

Showing day name and month name in ssrs

Evaluating the expression 1 <= month <= 12

Return a list of month numbers starting a few months back

Application where user inputs month number 1-12 and http post returns month name isn't working

SELECT last 13 months name inducing current Month