本文介绍了如何在Python中将一列数字转换为数据框中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据框包含一列日期.看起来像这样:

My dataframe contains a column for dates. It looks like this:

Index    Date
 0       12018
 1      102018
 2       32018
 3      122018
 4      112019
 5       32019
 6       42019

最后四个数字表示年份,第一个(两个)月份.我想将列更改为:

The last four numbers show the year and the first (two) the month.I want to change the column to:

- 01-01-2018
- 01-01-2018
- 01-10-2018
- 01-03-2018
...

甚至更好的日期时间格式.

or even better to a datetime format.

我已经尝试过此功能,该功能显示:

I've tried this function, which displays:

def adjust_date(dataset_in, col_name):
day = "01"
for col in col_name:
    if len(col_name)>5:
        month = col_name[0:1]
        year = col_name[2:5]
    else:
        month = col_name[0]
        year = col_name[1:4]

    result = year + "-" + month + "-" + day
return result

推荐答案

我认为格式的"nofollow noreferrer> to_datetime 应该足够了:

I think to_datetime with a specified format should be enough:

df['Date'] = pd.to_datetime(df['Date'], format='%m%Y')
print (df)
   Index       Date
0      0 2018-01-01
1      1 2018-10-01
2      2 2018-03-01
3      3 2018-12-01
4      4 2019-11-01
5      5 2019-03-01
6      6 2019-04-01

print (df.dtypes)
Index             int64
Date     datetime64[ns]
dtype: object

感谢@Vivek Kalyanarangan解决方案-添加 strftime 用于自定义string格式(但丢失了日期时间):

Thank you @Vivek Kalyanarangan for solution - add strftime for custom string format (but lost datetimes):

df['Date'] = pd.to_datetime(df['Date'], format='%m%Y').dt.strftime('%d-%m-%Y')
print (df)
   Index        Date
0      0  01-01-2018
1      1  01-10-2018
2      2  01-03-2018
3      3  01-12-2018
4      4  01-11-2019
5      5  01-03-2019
6      6  01-04-2019

print (df.dtypes)
Index     int64
Date     object
dtype: object

print (df['Date'].apply(type))
0    <class 'str'>
1    <class 'str'>
2    <class 'str'>
3    <class 'str'>
4    <class 'str'>
5    <class 'str'>
6    <class 'str'>
Name: Date, dtype: object

这篇关于如何在Python中将一列数字转换为数据框中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 19:35
查看更多