python/pandas:将月份整数转换为月份名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37625334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:40:54  来源:igfitidea点击:

python/pandas: convert month int to month name

pythondatepandasdataframemonthcalendar

提问by Boosted_d16

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

我发现的大部分信息不在 python>pandas>dataframe 中,因此是问题。

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

我想将 1 到 12 之间的整数转换为缩写的月份名称。

I have a df which looks like:

我有一个 df 看起来像:

   client Month
1  sss    02
2  yyy    12
3  www    06

I want the df to look like this:

我希望 df 看起来像这样:

   client Month
1  sss    Feb
2  yyy    Dec
3  www    Jun

回答by EoinS

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

您可以通过组合calendar.month_abbrdf[col].apply()

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

回答by today

Since the abbreviated month names is the first three letters of their full names, we could first convert the Monthcolumn to datetimeand 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:

由于月份缩写是他们全名的前三个字母,我们可以先将Month列转换为datetime然后使用dt.month_name()得到完整的月份名称,最后使用str.slice()方法获得前三个字母,全部使用pandas并且只在一行代码:

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

回答by jpp

The calendarmodule is useful, but calendar.month_abbris 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:

calendar模块很有用,但calendar.month_abbr类似于数组:它不能以矢量化方式直接使用。为了有效的映射,您可以构造一个字典,然后使用pd.Series.map

import calendar
d = dict(enumerate(calendar.month_abbr))
df['Month'] = df['Month'].map(d)

Performance benchmarking shows a ~130x performance differential:

性能基准测试显示约 130 倍的性能差异:

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

回答by Suhas_Pote

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

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

Reference:

参考:

回答by andrew

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

回答by pekapa

One way of doing that is with the applymethod 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.

一种方法是使用apply数据框中的方法,但是,要做到这一点,您需要一个地图来转换月份。您可以使用函数/字典或 Python 自己的日期时间来做到这一点。

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 lambdadirectly in the apply method.

显然,您不需要显式定义此函数,可以lambda直接在 apply 方法中使用 a 。

回答by Vagee

Use strptimeand lambdafunction for this:

用途strptimelambda功能:

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

回答by Heather

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)

回答by Nurul Akter Towhid

You can use Pandas month_name()function. Example:

您可以使用 Pandasmonth_name()功能。例子:

>>> idx = pd.date_range(start='2018-01', freq='M', periods=3)
>>> idx
DatetimeIndex(['2018-01-31', '2018-02-28', '2018-03-31'],
                  dtype='datetime64[ns]', freq='M')
>>> idx.month_name()
Index(['January', 'February', 'March'], dtype='object')

For more detail visit this link.

有关更多详细信息,请访问此链接