python/pandas: convert month int to month name
Solution 1
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])
Solution 2
Since the abbreviated month names is the first three letters of their full names, we could first convert the Month
column to datetime
and then use dt.month_name()
to get the full month name and finally use str.slice()
method to get the first three letters, all using pandas and only in one line of code:
df['Month'] = pd.to_datetime(df['Month'], format='%m').dt.month_name().str.slice(stop=3)
df
Month client
0 Feb sss
1 Dec yyy
2 Jun www
Solution 3
The calendar
module is useful, but calendar.month_abbr
is array-like: it cannot be used directly in a vectorised fashion. For an efficient mapping, you can construct a dictionary and then use pd.Series.map
:
import calendar
d = dict(enumerate(calendar.month_abbr))
df['Month'] = df['Month'].map(d)
Performance benchmarking shows a ~130x performance differential:
import calendar
d = dict(enumerate(calendar.month_abbr))
mapper = calendar.month_abbr.__getitem__
np.random.seed(0)
n = 10**5
df = pd.DataFrame({'A': np.random.randint(1, 13, n)})
%timeit df['A'].map(d) # 7.29 ms per loop
%timeit df['A'].map(mapper) # 946 ms per loop
Solution 4
You can do this easily with a column apply.
import pandas as pd
df = pd.DataFrame({'client':['sss', 'yyy', 'www'], 'Month': ['02', '12', '06']})
look_up = {'01': 'Jan', '02': 'Feb', '03': 'Mar', '04': 'Apr', '05': 'May',
'06': 'Jun', '07': 'Jul', '08': 'Aug', '09': 'Sep', '10': 'Oct', '11': 'Nov', '12': 'Dec'}
df['Month'] = df['Month'].apply(lambda x: look_up[x])
df
Month client
0 Feb sss
1 Dec yyy
2 Jun www
Solution 5
def mapper(month):
return month.strftime('%b')
df['Month'] = df['Month'].apply(mapper)
Reference:
Boosted_d16
Python and PowerPoints. The 2 most important Ps in the world.
Updated on July 09, 2022Comments
-
Boosted_d16 almost 2 years
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
-
jpp over 5 yearsNote this solution is akin to
list.__getitem__
in a Python-level loop, i.e. it does not take advantage of vectorised functionality available to Pandas. Extracting to a dictionary and then mapping is much more efficient, as per this answer. -
Nurul Akter Towhid over 4 yearsI don't think it's a good idea while you have pandas own month_name() function.
-
tdy over 2 yearsnote that
.str.slice(stop=3)
can be reduced to just.str[:3]