gpt4 book ai didi

python / Pandas : convert month int to month name

转载 作者:IT老高 更新时间:2023-10-28 20:59:18 27 4
gpt4 key购买 nike

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

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

我有一个 df,它看起来像:

   client Month
1 sss 02
2 yyy 12
3 www 06

我希望 df 看起来像这样:

   client Month
1 sss Feb
2 yyy Dec
3 www Jun

最佳答案

您可以通过结合使用 calendar.month_abbrdf[col].apply()

有效地做到这一点
import calendar
df['Month'] = df['Month'].apply(lambda x: calendar.month_abbr[x])

关于 python / Pandas : convert month int to month name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37625334/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com