在 pandas 中将月份从数字重命名为名称

2024-01-30

我有以下数据框:

High    Low Open    Close   Volume  Adj Close   year    pct_day
month   day                             
1   1   NaN NaN NaN NaN NaN NaN 2010.0  0.000000
2   7869.853149 7718.482498 7779.655014 7818.089966 7.471689e+07    7818.089966 2010.0  0.007826
3   7839.965652 7719.758224 7775.396255 7777.940002 8.185879e+07    7777.940002 2010.0  0.002582
4   7747.175260 7624.540007 7691.152083 7686.288672 1.018877e+08    7686.288672 2010.0  -0.000744
5   7348.487095 7236.742135 7317.313616 7287.688546 1.035424e+08    7287.688546 2010.0  -0.002499
... ... ... ... ... ... ... ... ... ...
12  27  7849.846680 7760.222526 7810.902051 7798.639258 4.678145e+07    7798.639258 2009.5  -0.000833
28  7746.209996 7678.152204 7713.497907 7710.449358 4.187133e+07    7710.449358 2009.5  0.000578
29  7357.001540 7291.827806 7319.393874 7338.938345 4.554891e+07    7338.938345 2009.5  0.003321
30  7343.726938 7276.871507 7322.123779 7302.545316 3.967812e+07    7302.545316 2009.5  -0.000312
31  NaN NaN NaN NaN NaN NaN 2009.5  0.000000

由于从上面粘贴的数据框中不清楚,下面是一个快照:

月份格式为 1,2 3 ... 是否可以将月份索引重命名为 Jan Feb Mar 格式?

Edit :

我很难实现@ChihebNexus 的示例

我的代码如下,因为它是日期时间:

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()

我会使用 calendar 和 pd.CategoricalDtype 来确保排序正确进行。

import pandas as pd
import numpy as np
import calendar

#Create dummy dataframe
dateindx = pd.date_range('2019-01-01', '2019-12-31', freq='D')

df = pd.DataFrame(np.random.randint(0,1000, (len(dateindx), 5)), 
             index=pd.MultiIndex.from_arrays([dateindx.month, dateindx.day]),
             columns=['High', 'Low','Open', 'Close','Volume'])

#Use calendar library for abbreviations and order
dd=dict((enumerate(calendar.month_abbr)))

#rename level zero of multiindex
df = df.rename(index=dd,level=0)

#Create calendar month data type with order for sorting
cal_dtype = pd.CategoricalDtype(list(calendar.month_abbr), ordered=True)

#Change the dtype of the level zero index
df.index = df1.index.set_levels(df.index.levels[0].astype(cal_dtype), level=0)
df

Output:

        High  Low  Open  Close  Volume
Jan 1    501  720   671    943     586
    2    410   67   207    945     284
    3    473  481   527    415     852
    4    157  809   484    592     894
    5    294   38   458     62     945
...      ...  ...   ...    ...     ...
Dec 27   305  354   347      0     726
    28   764  987   564    260      72
    29   730  151   846    137     118
    30   999  399   634    674      81
    31   347  980   441    600     676

[365 rows x 5 columns]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 pandas 中将月份从数字重命名为名称 的相关文章

随机推荐