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

标签 python

我有一列将日期显示为 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/

相关文章:

Python:如何在距离以米为单位的点周围创建方形缓冲区

python - 数据框未使用值进行更新;输出是警告 'copy of a slide'

python - 为什么我的 Django URL 调度程序不工作?

python - 如何接收 STOMP 中丢失的消息?

python - 我应该如何为 R 和 Octave 方法(可能使用 Python)进行快速 GUI 开发?

python - Python 替换函数中的 "search for"参数是否有限制?

python - 根据相关字段最早实例的值过滤 Django 查询集

python - 停止 Python Multiprocessing BaseManager 'serve_forever' 服务器?

python - 使用单独的数据框将多索引映射到现有的 pandas 数据框列

Python/Yagmail - 如何将本地镜像嵌入到电子邮件中?