Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Pandas Index to month name

Tags:

python

pandas

I am trying to change my Date index in the following time series to the month name.

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

for example I want it to look like this:

    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

I want to be able to this so my plot ticks will be the month name instead of the datetime.

Thanks

edit//

same scenario but solution does not work on it:

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

gives me an error

AttributeError: 'str' object has no attribute 'strftime'
like image 287
collarblind Avatar asked Sep 21 '15 16:09

collarblind


People also ask

How do you get the month name in pandas?

Pandas dt. month_name() function returns the month names of the DateTimeIndex with specified locale. Locale determines the language in which the month name is returned.

How do I get the month name from a number in Python?

Method #1 : Using strftime() + %B In this, we use strftime() which converts date object to a string using a format, and by providing %B, it's enforced to just return a Month Name.

How do you name an index in pandas?

You can use the rename() method of pandas. DataFrame to change column/index name individually. Specify the original name and the new name in dict like {original name: new name} to columns / index parameter of rename() . columns is for the column name, and index is for the index name.


2 Answers

If you have a DatetimeIndex, you can use

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

.rename can take a function, and we'll use the '%B' code for the full month name.

like image 66
TomAugspurger Avatar answered Oct 01 '22 20:10

TomAugspurger


Use 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

Also for assign new values in index is possible use 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

EDIT:

For versions pandas 0.23.0 is possible use 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
like image 41
jezrael Avatar answered Oct 01 '22 20:10

jezrael