如何将Pandas Index转换为月份名称

时间:2015-09-21 16:14:58

标签: python pandas

我正在尝试将以下时间序列中的日期索引更改为月份名称。

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'

4 个答案:

答案 0 :(得分:4)

如果您有DatetimeIndex,则可以使用

websiteGroup.rename(index=lambda x: x.strftime('%B'))

.rename可以使用某个功能,我们会将'%B'代码用作完整的月份名称。

答案 1 :(得分:1)

使用DatetimeIndex.strftime

websiteGroup.index = websiteGroup.index.strftime('%B')
print (websiteGroup)
               A        B       C
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.0
df = websiteGroup.set_index(websiteGroup.index.strftime('%b'))
print (df)
          A        B       C
Jan   18185   805769     NaN
Feb   73236   944458     NaN
Mar  101737  1003966     NaN
Apr  101018   861229     NaN
May   77724   845223     NaN
Jun  111503   966043     NaN
Jul  115413   937184     NaN
Aug  115215   890457  1649.0

另外,为了在索引中分配新值,可以使用set_index

df = websiteGroup.set_index(websiteGroup.index.strftime('%B'))
print (df)
               A        B       C
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.0

编辑:

对于版本pandas 0.23.0,可以使用DatetimeIndex.month_name

websiteGroup.index = websiteGroup.index.month_name()
print (websiteGroup)
               A        B       C
Website                          
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.0

答案 2 :(得分:0)

您可以使用datetime.strptime解析每个日期字符串,并使用datetime.strftime('%B')打印月份名称:

>>> d = datetime.datetime.strptime('2015-01-01', '%Y-%m-%d')
>>> d.strftime('%B')
'January'

答案 3 :(得分:0)

Pandas版本0.23.0及更高版本(在撰写本文时为0.24.2)提供了内置方法:std::move。来自its official documentation

  

.month_name返回具有指定语言环境的DateTimeIndex的月份名称。

考虑以下DataFrame:

pandas.DatetimeIndex.month_name

DataFrame具有DateTimeIndex,因此我们可以像这样在索引上应用aapl.tail() # returns: Attributes High Low Open Close Volume Adj Close 2019-03-27 189.76 186.55 188.75 188.47 29848400.0 188.47 2019-03-28 189.56 187.53 188.95 188.72 20780400.0 188.72 2019-03-29 190.08 188.54 189.83 189.95 23564000.0 189.95 2019-03-30 190.08 188.54 189.83 189.95 23564000.0 189.95 2019-03-31 190.08 188.54 189.83 189.95 23564000.0 189.95

.month_name

在0.23.0之前,您将使用aapl.index = aapl.index.month_name() aapl.tail() # returns: Attributes High Low Open Close Volume Adj Close March 189.76 186.55 188.75 188.47 29848400.0 188.47 March 189.56 187.53 188.95 188.72 20780400.0 188.72 March 190.08 188.54 189.83 189.95 23564000.0 189.95 March 190.08 188.54 189.83 189.95 23564000.0 189.95 March 190.08 188.54 189.83 189.95 23564000.0 189.95 ,而其他引用使用.month()的答案是可行的方法。