python/pandas: convert month int to month name

Boosted_d16 picture Boosted_d16 · Jun 4, 2016 · Viewed 70.2k times · Source

Most of the info I found was not in python>pandas>dataframe hence the question.

I want to transform an integer between 1 and 12 into an abbrieviated month name.

I have a df which looks like:

   client Month
1  sss    02
2  yyy    12
3  www    06

I want the df to look like this:

   client Month
1  sss    Feb
2  yyy    Dec
3  www    Jun

Answer

EoinS picture EoinS · Jun 4, 2016

You can do this efficiently with combining calendar.month_abbr and df[col].apply()

import calendar
df['Month'] = df['Month'].apply(lambda x: calendar.month_abbr[x])