python - 如何将数据框的日期和时间列转换为 Pandas 的日期时间格式?

标签 python pandas datetime

我的数据框 df 中有一个 DateTime 列,如下所示:

   DateTime
3/1/2016 12:15:00 AM    
3/1/2016 12:30:00 AM    
3/1/2016 12:45:00 AM    
3/1/2016 1:00:00 AM 
3/1/2016 1:15:00 AM 
3/1/2016 1:30:00 AM 
3/1/2016 1:45:00 AM 
3/1/2016 2:00:00 AM     
3/1/2016 2:15:00 AM 

我想将其转换为以下格式,即 24 小时格式,如下所示:

03-01-2016 12:15:00

我该怎么做?

最佳答案

这应该有效:

df["DateTime"] = pd.to_datetime(df["DateTime"],errors="coerce").dt.strftime("%d-%m-%Y %H:%M:%S")

print (df["DateTime"])

输出:

0    01-03-2016 00:15:00
1    01-03-2016 00:30:00
2    01-03-2016 00:45:00
3    01-03-2016 01:00:00
4    01-03-2016 01:15:00
5    01-03-2016 01:30:00
6    01-03-2016 01:45:00
7    01-03-2016 02:00:00
8    01-03-2016 02:15:00
Name: DateTime, dtype: object

关于python - 如何将数据框的日期和时间列转换为 Pandas 的日期时间格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42524793/

相关文章:

python Pandas : Create a new column with values in English by converting values stored in a different column in Chinese traditional

python - 将 Pandas 导入 Python

python - 如何在多索引数据框 Python 中添加/删除索引

Python从元组列表中生成字典列表

python - Pandas : remove SOME duplicate values based on conditions

Python 月份开始日期和结束日期之间的两个日期

php - mysql数据库的时区问题

vba - 查找两个日期的小时和分钟差异,格式为 dd/mm/yyyy hh :mm

python - Pandas 中一列的值作为列,同时保持最后更新

python - os.fork() sem_wait : Permission denied