简体   繁体   English

将不同的日期格式转换为数据框列中的单一格式

[英]Converting different date formats to a single format in a dataframe column

I want to make different date formats the same in my dataframe column.我想在我的数据框列中使不同的日期格式相同。

input:输入:

0      16.09.2014
1      2014-09-16
2      02.12.2014
3      2014-12-02
4      09.01.2018

183    2015-03-30
184    12.04.2017
185    2017-04-12
186    28.12.2018
187    2018-12-28
Name: invoiceDate, Length: 188, dtype: object

but I use:但我使用:

df['invoiceDate'] = pd.to_datetime(df['invoiceDate'], errors='coerce') 
df['invoiceDate'].dt.strftime('%d.%m.%Y') 

output: (it writes the first two in the same format but then it goes wrong and gets month by day and month by month.)输出:(它以相同的格式写入前两个,但随后出错并逐月逐月获取。)

0      16.09.2014
1      16.09.2014
2      12.02.2014 (true)
3      02.12.2014 (false)
4      01.09.2018
   
183    30.03.2015
184    04.12.2017 (true)
185    12.04.2017 (false)
186    28.12.2018
187    28.12.2018
Name: invoiceDate, Length: 188, dtype: object

If the output should be;如果输出应该是;

output :输出 :

0      16.09.2014
1      16.09.2014
2      02.12.2014
3      02.12.2014
4      09.01.2018

183    30.03.2015
184    12.04.2017
185    12.04.2017
186    28.12.2018
187    28.12.2018
Name: invoiceDate, Length: 188, dtype: object

In this particular case, you just need to pass dayfirst=True to parse the dates as you intend在这种特殊情况下,您只需要传递dayfirst=True即可按照您的意图解析日期

df['invoiceDate'] = (
    pd.to_datetime(df['invoiceDate'], errors='coerce', dayfirst=True)
      .dt.strftime('%d.%m.%Y')
)

If you set the column of the dates to = date you could do the following如果您将日期列设置为 = date,您可以执行以下操作

df['Date'] = pd.to_datetime(df['Date'], infer_datetime_format=True)

from here you can simply tell it the format you want the date to be structured using a strftime从这里您可以简单地告诉它您希望使用 strftime 构造日期的格式

df['Date'] = df['Date'].apply(lambda x : x.strftime('%d.%m.%Y'))

暂无
暂无

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

相关问题 将不同的日期格式转换为一种格式时出现值错误 - Value error when converting different date formats to one single format Dataframe 具有不同格式的日期值的字符串列。 如何将整列数据转换为单个日期格式? - Dataframe has string column with date values in different formats. how to convert entire column data to single date format? 在pandas数据帧中将不同的日期时间格式转换为MM / DD / YYYY格式 - Converting different date time formats to MM/DD/YYYY format in pandas dataframe 如何将 pandas dataframe 列中的两种不同日期格式转换为相同格式? - How to convert two different date formats from a pandas dataframe column into same format? 在Python的数据框列中将两种日期格式转换为一种 - Converting two date formats to one in a dataframe column in Python 在熊猫数据框中转换日期格式 - Converting date formats in pandas dataframe 如何将熊猫数据框日期和不同的时间格式连接到单个时间戳? - How to concatenate pandas dataframe date and different time formats to single timestamp? 将 Python 数据框列转换为日期格式 - Converting Python dataframe column to date format 当列中已存在多种日期格式时,更改整个 Dataframe 列的日期格式? - Changing the date format of an entire Dataframe column when multiple date formats already exist in the column? 将具有多种日期格式的 dataframe 行转换为日期时间 - Converting a dataframe row with multiple date formats into a datetime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM