简体   繁体   English

将日期转换为整个新列的不同格式

[英]Convert a date to a different format for an entire new column

I want to convert the date in a column in a dataframe to a different format.我想将数据框中列中的日期转换为不同的格式。 Currently, it has this format: '2019-11-20T01:04:18' .目前,它具有以下格式: '2019-11-20T01:04:18' I want it to have this format: 20-11-19 1:04 .我希望它具有这种格式: 20-11-19 1:04

I think I need to develop a loop and generate a new column for the new date format.我想我需要开发一个循环并为新的日期格式生成一个新列。 So essentially, in the loop, I would refer to the initial column and then generate the variable for the new column in the format I want.所以本质上,在循环中,我会引用初始列,然后以我想要的格式为新列生成变量。

Can someone help me out to complete this task?有人可以帮我完成这项任务吗?

The following code works for one occasion:以下代码适用于一种情况:

import datetime
d = datetime.datetime.strptime('2019-11-20T01:04:18', '%Y-%m-%dT%H:%M:%S')
print d.strftime('%d-%m-%y %H:%M')

From a previous answer in this site , this should be able to help you, comments give explanation You can read your data into pandas from csv or database or create some test data as shown below for testing.本站点先前答案中,这应该可以帮助您,评论给出解释您可以将数据从 csv 或数据库读入 Pandas 或创建一些测试数据,如下所示进行测试。

>>> import pandas as pd
>>> df = pd.DataFrame({'column': {0: '26/1/2016', 1: '26/1/2016'}})
>>> # First convert the column to datetime datatype
>>> df['column'] = pd.to_datetime(df.column)
>>> # Then call the datetime object format() method, set the modifiers you want here
>>> df['column'] = df['column'].dt.strftime('%Y-%m-%dT%H:%M:%S') 

>>> df
                 column
0  2016-01-26T00:00:00
1  2016-01-26T00:00:00

NB.注意。 Check to ensure that all your columns have similar date strings检查以确保所有列都具有相似的日期字符串

You can either achieve it like this:您可以像这样实现它:

from datetime import datetime

df['your_column'] = df['your_column'].apply(lambda x: datetime.strptime(x, '%Y-%m-%dT%H:%M:%S').strftime('%d-%m-%y %H:%M'))

暂无
暂无

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

相关问题 Dataframe 具有不同格式的日期值的字符串列。 如何将整列数据转换为单个日期格式? - Dataframe has string column with date values in different formats. how to convert entire column data to single date format? 需要将整个列从字符串格式转换为数据帧中的日期格式 - Need to convert entire column from string format to date format from a Dataframe 将不同的日期格式字符串转换为日期时间格式 - Convert different date format string into datetime format 将数据帧中的诸如201729之类的Week格式转换为日期为第一个星期一的新列中的月份 - Convert a Week format like 201729 in a dataframe to a month in a new column where the date is the first monday 如何将不同日期格式的字符串转换为python中的单一日期格式? - how to convert sting of different date format in to single date format in python? 如何为python数据框中的整个列创建日期格式? - How do I make a date format for an entire column in python dataframe? 如何将 pandas dataframe 列中的两种不同日期格式转换为相同格式? - How to convert two different date formats from a pandas dataframe column into same format? 如何将不同的日期格式转换为一种 - How to convert different date format to one 在熊猫数据框中将字符串日期转换为其他格式 - Convert string date to a different format in pandas dataframe 在Python中将一列转换为标准日期格式 - Convert one column to standard date format in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM