python:从年-月-日的列中获取日期的列 [英] python: obtaining a column of dates from the columns of years-months-days

查看:267
本文介绍了python:从年-月-日的列中获取日期的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个非常简单的数据框:

Suppose I have a very simple dataframe:

>>> a
Out[158]: 
   monthE  yearE dayE
0      10   2014   15
1       2   2012   15
2       2   2014   15
3      12   2015   15
4       2   2012   15

假设我要使用三列整数创建具有与每一行相关的日期的列. 当我有简单的数字时,就可以这样做:

Suppose that I want to create the column with the date related to every line, using three columns of integers. When I have simple numbers it is enough to do like:

>>> datetime.date(1983,11,8)
Out[159]: datetime.date(1983, 11, 8)

如果我必须创建一列日期(理论上是一个非常基本的请求),则代替:

If I have to create a column of dates (theoretically a very basic request), instead:

a.apply(lambda x: datetime.date(x['yearE'],x['monthE'],x['dayE']))

我得到以下错误:

KeyError :("yearE",u发生在索引monthE')

KeyError: ('yearE', u'occurred at index monthE')

推荐答案

我认为您可以先删除最后一个字符E,然后使用

I think you can first remove last char E and then use to_datetime, but then get pandas timestamps not python dates:

df.columns = df.columns.str[:-1]
df['date'] = pd.to_datetime(df)
#if multiple columns filter by subset
#df['date'] = pd.to_datetime(df[['year','month','day']])
print (df)
   month  year  day       date
0     10  2014   15 2014-10-15
1      2  2012   15 2012-02-15
2      2  2014   15 2014-02-15
3     12  2015   15 2015-12-15
4      2  2012   15 2012-02-15

print (df.date.dtypes)
datetime64[ns]

print (df.date.iloc[0])
2014-10-15 00:00:00

print (type(df.date.iloc[0]))
<class 'pandas.tslib.Timestamp'>

谢谢您 MaxU 作为解决方案:

Thank you MaxU for solution:

df['date'] = pd.to_datetime(df.rename(columns = lambda x: x[:-1]))
#if another columns in df
#df['date'] = pd.to_datetime(df[['yearE','monthE','dayE']].rename(columns=lambda x: x[:-1]))
print (df)
   monthE  yearE  dayE       date
0      10   2014    15 2014-10-15
1       2   2012    15 2012-02-15
2       2   2014    15 2014-02-15
3      12   2015    15 2015-12-15
4       2   2012    15 2012-02-15

但如果确实需要python dates,则将axis=1添加到

But if really need python dates add axis=1 to apply, but then is impossible use some pandas functions:

df['date'] =df.apply(lambda x: datetime.date(x['yearE'],x['monthE'],x['dayE']), axis=1)
print (df)
   monthE  yearE  dayE        date
0      10   2014    15  2014-10-15
1       2   2012    15  2012-02-15
2       2   2014    15  2014-02-15
3      12   2015    15  2015-12-15
4       2   2012    15  2012-02-15

print (df.date.dtypes)
object

print (df.date.iloc[0])
2014-10-15

print (type(df.date.iloc[0]))
<class 'datetime.date'>

这篇关于python:从年-月-日的列中获取日期的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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