如何将年,月和日列合并为单个datetime列? [英] How to combine year, month, and day columns to single datetime column?

查看:193
本文介绍了如何将年,月和日列合并为单个datetime列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框df:

        id  lat        lon      year    month   day         
0       381 53.30660   -0.54649 2004    1       2       
1       381 53.30660   -0.54649 2004    1       3            
2       381 53.30660   -0.54649 2004    1       4   

,我想创建一个新列df['Date'],其中yearmonthday列根据格式yyyy-m-d组合在一起.

and I want to create a new column df['Date'] where the year, month, and day columns are combined according to the format yyyy-m-d.

这篇文章之后,我做到了:

`df['Date']=pd.to_datetime(df['year']*10000000000
                           +df['month']*100000000
                           +df['day']*1000000,
                           format='%Y-%m-%d%')`

结果不是我期望的,因为它从1970年开始而不是2004年开始,并且还包含时标,我没有指定:

The result is not what I expected, as it starts from 1970 instead of 2004, and it also contains the hour stamp, which I did not specify:

        id  lat        lon      year    month   day  Date           
0       381 53.30660   -0.54649 2004    1       2    1970-01-01 05:34:00.102    
1       381 53.30660   -0.54649 2004    1       3    1970-01-01 05:34:00.103         
2       381 53.30660   -0.54649 2004    1       4    1970-01-01 05:34:00.104

由于日期应采用2004-1-2格式,我在做什么错了?

As the dates should be in the 2004-1-2 format, what am I doing wrong?

推荐答案

有一种更简单的方法:

In [250]: df['Date']=pd.to_datetime(df[['year','month','day']])

In [251]: df
Out[251]:
    id      lat      lon  year  month  day       Date
0  381  53.3066 -0.54649  2004      1    2 2004-01-02
1  381  53.3066 -0.54649  2004      1    3 2004-01-03
2  381  53.3066 -0.54649  2004      1    4 2004-01-04

来自文档:

从DataFrame的多个列中组合一个日期时间.按键 可以是常见的缩写,例如[yearmonthdayminutesecondmsusns])或多个相同的

Assembling a datetime from multiple columns of a DataFrame. The keys can be common abbreviations like [year, month, day, minute, second, ms, us, ns]) or plurals of the same

这篇关于如何将年,月和日列合并为单个datetime列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆