Skip to content
Advertisement

Convert month int to month name in Pandas

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

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

Advertisement

Answer

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])
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement