用 pandas 将月份格式化为个位数

问题描述 投票:0回答:1

我有以下代码

import pandas as pd
pd.to_datetime(['8/23/1999']).strftime("%m/%d/%Y").astype('str')

这会生成

08/23/1999

但是我想得到

8/23/1999

这种情况有什么特定的格式可以使用吗?

python-3.x pandas
1个回答
1
投票

Pandas 支持 Glibc 扩展(参见

man 3 strftime
),因此您可以使用
-
标志。 例如

pd.to_datetime(['8/23/1999']).strftime("%-m/%d/%Y").astype('str')
© www.soinside.com 2019 - 2024. All rights reserved.