我正在尝试将以下时间序列中的日期索引更改为月份名称。
website = dfFinal.groupby(['Date','Website'])
websiteGroup = website['Visits'].aggregate(np.sum).unstack()
Website A B C
Date
2015-01-01 18185 805769 NaN
2015-02-01 73236 944458 NaN
2015-03-01 101737 1003966 NaN
2015-04-01 101018 861229 NaN
2015-05-01 77724 845223 NaN
2015-06-01 111503 966043 NaN
2015-07-01 115413 937184 NaN
2015-08-01 115215 890457 1649
例如,我希望它看起来像这样:
Website A B C
Date
January 18185 805769 NaN
February 73236 944458 NaN
March 101737 1003966 NaN
April 101018 861229 NaN
May 77724 845223 NaN
June 111503 966043 NaN
July 115413 937184 NaN
August 115215 890457 1649
我希望能够做到这一点,因此我的图表刻度将是月份名称而不是日期时间。
谢谢
编辑//
相同的情况,但解决方案不起作用:
systemType = dfFinal.groupby(['Date','Website','Type'])
systemGroup = systemType['Visits'].aggregate(np.sum)
systemGroup = systemGroup.groupby(level=[0,1]).apply(lambda x: 100*x/float(x.sum())).unstack()
Type Other Windows Mobile Windows PC
Date Website
2015-01-01 A 0.637888 0.005499 48.814957
B 0.686549 0.016506 54.176073
2015-02-01 A 0.742804 0.020482 49.811568
B 0.651802 0.014506 57.014288
2015-03-01 A 0.668390 0.014744 50.087972
B 0.573924 0.015937 59.906013
2015-04-01 A 0.662258 0.015839 49.310024
B 0.583933 0.013469 59.490449
2015-05-01 A 0.666461 0.020586 48.522979
B 0.577954 0.017983 58.838200
systemGroup = systemGroup.rename(index=lambda x: x.strftime('%B'))
给我一个错误
AttributeError: 'str' object has no attribute 'strftime'
最佳答案
如果您有DatetimeIndex,则可以使用
websiteGroup.rename(index=lambda x: x.strftime('%B'))
.rename
可以使用一个函数,我们将使用'%B'
代码作为完整的月份名称。关于python - 如何将 Pandas 指数转换为月份名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32699950/