python/pandas: convert month int to month name

纵然是瞬间 提交于 2019-11-27 04:05:17

问题


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

回答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])



回答2:


One way of doing that is with the apply method in the dataframe but, to do that, you need a map to convert the months. You could either do that with a function / dictionary or with Python's own datetime.

With the datetime it would be something like:

def mapper(month):
    date = datetime.datetime(2000, month, 1)  # You need a dateobject with the proper month
    return date.strftime('%b')  # %b returns the months abbreviation, other options [here][1]

df['Month'].apply(mapper)


In a simillar way, you could build your own map for custom names. It would look like this:

months_map = {01: 'Jan', 02: 'Feb'}
def mapper(month):
    return months_map[month]


Obviously, you don't need to define this functions explicitly and could use a lambda directly in the apply method.




回答3:


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



回答4:


Use strptime and lambda function for this:

from time import strptime
df['Month'] = df['Month'].apply(lambda x: strptime(x,'%b').tm_mon) 



回答5:


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



回答6:


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



回答7:


def mapper(month):
   return month.strftime('%b') 

df['Month'] = df['Month'].apply(mapper)

Reference:

  • http://strftime.org/



回答8:


Having tested all of these on a large dataset, I have found the following to be fastest:

import calendar
def month_mapping():
    # I'm lazy so I have a stash of functions already written so
    # I don't have to write them out every time. This returns the
    # {1:'Jan'....12:'Dec'} dict in the laziest way...
    abbrevs = {}
    for month in range (1, 13):
        abbrevs[month] = calendar.month_abbr[month]
    return abbrevs

abbrevs = month_mapping()

df['Month Abbrev'} = df['Date Col'].dt.month.map(mapping)


来源:https://stackoverflow.com/questions/37625334/python-pandas-convert-month-int-to-month-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!