gpt4 book ai didi

python以日期格式更改月和日的顺序

转载 作者:太空宇宙 更新时间:2023-11-03 15:48:38 25 4
gpt4 key购买 nike

我有一列将日期显示为2016/26/2 (日/月/年)

如何将其显示为 2/26/2016(月/日/年)?

[in] print(nir['Date'])
[out] 0 2005-01-12
1 2006-01-03
2 2006-01-06
3 2006-01-09
4 2006-01-12
5 2007-01-03
6 2007-01-06
7 2007-01-09
8 2007-01-12

我试着这样做:

      nir.Date = ['{}-{}-{}'.format(m,d,y) for y, m, d in map(lambda x: str(x).split('-'), nir.Date)]

但运气不好...有什么方法可以在不转换为字符串的情况下轻松交换月份和日期的位置?

最佳答案

python/pandas 中,如果不想转换为字符串是不可能的。

对于字符串使用 strftimeformats :

nir.Date = nir.Date.dt.strftime('%m/%d/%Y')

关于python以日期格式更改月和日的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48238400/

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