简体   繁体   English

将日期时间数据的数据框列转换为 DD/MM/YYYY 字符串数据

[英]Converting dataframe column of datetime data to DD/MM/YYYY string data

I have a dataframe column with datetime data in 1980-12-11T00:00:00 format.我有一个包含1980-12-11T00:00:00格式的日期时间数据的数据1980-12-11T00:00:00列。

I need to convert the whole column to DD/MM/YYY string format.我需要将整列转换为DD/MM/YYY字符串格式。

Is there any easy code for this?有没有简单的代码?

Creating a working example:创建一个工作示例:

df = pd.DataFrame({'date':['1980-12-11T00:00:00', '1990-12-11T00:00:00', '2000-12-11T00:00:00']})
print(df)

                  date
0  1980-12-11T00:00:00
1  1990-12-11T00:00:00
2  2000-12-11T00:00:00

Convert the column to datetime by pd.to_datetime() and invoke strftime()通过pd.to_datetime()将列转换为datetime pd.to_datetime()并调用strftime()

df['date_new']=pd.to_datetime(df.date).dt.strftime('%d/%m/%Y')
print(df)

                  date    date_new
0  1980-12-11T00:00:00  11/12/1980
1  1990-12-11T00:00:00  11/12/1990
2  2000-12-11T00:00:00  11/12/2000

You can use pd.to_datetime to convert string to datetime data您可以使用pd.to_datetime将字符串转换为日期时间数据

pd.to_datetime(df['col'])

You can also pass specific format as:您还可以将特定格式传递为:

pd.to_datetime(df['col']).dt.strftime('%d/%m/%Y')

When using pandas , try pandas.to_datetime :使用pandas ,请尝试pandas.to_datetime

import pandas as pd
df = pd.DataFrame({'date': ['1980-12-%sT00:00:00'%i for i in range(10,20)]})
df.date = pd.to_datetime(df.date).dt.strftime("%d/%m/%Y")
print(df)
         date
0  10/12/1980
1  11/12/1980
2  12/12/1980
3  13/12/1980
4  14/12/1980
5  15/12/1980
6  16/12/1980
7  17/12/1980
8  18/12/1980
9  19/12/1980

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 日期时间列在 mm/dd/yyyy 中,需要 dd/mm/yyyy - DateTime column coming in mm/dd/yyyy, want dd/mm/yyyy 将长日期时间转换为日期 (dd/mm/yyyy) - Converting long datetime into date (dd/mm/yyyy) 检查python中数据框中的字符串是否为日期时间(mm-dd-yyyy)格式 - check if a string is in datetime (mm-dd-yyyy) format in a data frame in python Python日期字符串mm / dd / yyyy到datetime - Python date string mm/dd/yyyy to datetime 为什么 Pandas to_datetime() function 在同一数据系列 (YYYY-DD-MM) 和 (YYYY-MM-DD) 中以两种不同格式返回 (DD-MM-YYYY) 日期时间? - Why is Pandas to_datetime() function returning (DD-MM-YYYY) datetime in two different formats in the same data series (YYYY-DD-MM) and (YYYY-MM-DD)? 数据框列中的日期转换为MM / DD / YYYY - Conversion of dates in the dataframe column into MM/DD/YYYY Python:将小时数连接到数据文件中的dd / mm / yyyy列 - Python: Concatenate Hour into dd/mm/yyyy column in data file 将日期重新格式化为YYYY / MM / DD并替换CSV中的列数据 - reformat date to YYYY/MM/DD and replace column data in CSV 如何在 Python 数据帧中将时间戳列格式从 [YYYY/MM/DD] 转换为 [DD/MM/YYYY]? - How can I can convert a timestamp column format from [YYYY/MM/DD] to from [DD/MM/YYYY] in Python data frame? 在 python 中将字符串 (MM-DD) 转换为日期时间 - Converting string (MM-DD) into datetime in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM