You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python的Pandas DataFrame列中将月份整数转换为月份缩写名

Pandas将整数月份转为英文缩写名

方法一:使用Pandas内置日期处理函数

通过pd.to_datetime把整数转换为日期对象,再提取月份缩写:

import pandas as pd

# 构造示例DataFrame
df = pd.DataFrame({
    'Parcel': ['Property A', 'Property B', 'Property C'],
    'Month_sold': [8, 9, 2]
})

# 转换整数月份为英文缩写
df['Month_sold'] = pd.to_datetime(df['Month_sold'], format='%m').dt.strftime('%b')

print(df)

输出结果:

Parcel Month_sold
0  Property A        Aug
1  Property B        Sep
2  Property C        Feb

方法二:使用calendar模块

利用calendar.month_abbr列表直接映射整数到月份缩写(列表索引1对应Jan,索引12对应Dec):

import pandas as pd
import calendar

df = pd.DataFrame({
    'Parcel': ['Property A', 'Property B', 'Property C'],
    'Month_sold': [8, 9, 2]
})

# 映射整数到月份缩写
df['Month_sold'] = df['Month_sold'].apply(lambda x: calendar.month_abbr[x])

print(df)

该方法也会得到与期望一致的输出。

内容的提问来源于stack exchange,提问作者BannyM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 07:09:20