Renaming months from number to name in pandas


For example, if we could have this DataFrame, we could use datetime package within this datetime format table like this example:

import pandas as pd
from datetime import datetime df = pd.DataFrame(range(1, 13), columns=['month']) df['month'] = df.apply( lambda row: '{:%b}'.format(datetime.strptime(str(row['month']), '%m')), axis=1
) print(df)

Output:

0 Jan
1 Feb
2 Mar
3 Apr
4 May
5 Jun
6 Jul
7 Aug
8 Sep
9 Oct
10 Nov
11 Dec

Update: As @Ch3steR suggested. You're using a MultiIndex DataFrame. So, here is an example how you can modify it's first level index:

import pandas as pd
import numpy as np
from datetime import datetime tuples = [(1, 10), (1, 12), (1, 13), (2, 1), (2, 20), (2, 10)]
index = pd.MultiIndex.from_tuples(tuples, names=['month', 'day'])
serie = pd.Series(np.random.randn(len(tuples)), index=index)
df = pd.DataFrame(serie, columns=['data']) print(df) data
month day 1 10 -0.463804 12 1.979072 13 0.087430
2 1 0.928077 20 -0.697795 10 -0.275762 idx = pd.Index(df.index).get_level_values(0)
# Set new index, but keep the multindex levels
df = df.set_index(pd.MultiIndex.from_tuples((( '{:%b}'.format(datetime.strptime(str(k), '%m')), v ) for k, v in idx), names=['month', 'day']), ['month', 'day']) print(df) data
month day Jan 10 -0.463804 12 1.979072 13 0.087430
Feb 1 0.928077 20 -0.697795 10 -0.275762

Update2:

I see that you've hard time to implement my answer into your code. This is why i've making this update to show you how you can implement my code within the code snipped you've added to your question. This is an example:

from datetime import datetime
import pandas as pd start = '1/4/2020'
end = '3/5/2020' data = pd.DataFrame()
full_dates = pd.date_range(start, end)
data = data.reindex(full_dates)
data['year'] = data.index.year
data['month'] = data.index.month
data['week'] = data.index.week
data['day'] = data.index.day
data.set_index('month', append=True, inplace=True)
data.set_index('week', append=True, inplace=True)
data.set_index('day', append=True, inplace=True)
df = data.groupby(['month', 'day']).mean()
idx = pd.Index(df.index).get_level_values(0)
df = df.set_index(pd.MultiIndex.from_tuples((( '{:%b}'.format(datetime.strptime(str(k), '%m')), v
) for k, v in idx), names=['month', 'day']), ['month', 'day'])
print(df)

Output:

 year
month day Jan 4 2020 5 2020 6 2020 7 2020 8 2020
... ...
Mar 1 2020 2 2020 3 2020 4 2020 5 2020 [62 rows x 1 columns]