【发布时间】:2020-09-02 15:51:58
【问题描述】:
我有以下数据框:
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 格式?
编辑:
我很难实现@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()
【问题讨论】:
-
@Raju 您能否详细说明一下,我不确定您所说的替换命令是什么意思
-
@newcoder,你能更好地了解你的数据框吗?看起来您的年份是整数,而月份和日期是索引。我建议从这 3 列中创建一个日期,并使用
strftime从该新列中提取月份名称。看看docs.python.org/3/library/…。 -
df.replace({'month': {1:' jan', 2: 'feb',....}})
标签: python python-3.x pandas