gpt4 book ai didi

python - 将 pandas 数据框中的列转换为日期时间

转载 作者:行者123 更新时间:2023-12-01 01:31:12 24 4
gpt4 key购买 nike

  gps   nt_date 
4002 20-Dec-16
4002 20-Dec-16
C-4 30-Jan-17
Y21-21 17-Nov-16
49a 22-Dec-16

在上面的数据框中,如何将 nt_date 列转换为 pandas 日期时间?我正在使用这个,但它不起作用:

pd.to_datetime(df['nt_date'], format='%d-%m-%Y')

如何解决这个问题?

最佳答案

您使用的指令略有错误。尝试使用 %b (月份作为区域设置的缩写名称)和 %y (2 位数年份,而不是 4 位数的 %Y):

df['nt_date'] = pd.to_datetime(df['nt_date'], format='%d-%b-%y')

>>> df
gps nt_date
0 4002 2016-12-20
1 4002 2016-12-20
2 C-4 2017-01-30
3 Y21-21 2016-11-17
4 49a 2016-12-22

更多信息,您可以引用these docs

关于python - 将 pandas 数据框中的列转换为日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52846411/

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