问题描述
假设我有一个非常简单的数据框:
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']))
我得到以下错误:
推荐答案
我认为您可以先删除最后一个字符E
,然后使用 to_datetime
,但随后得到的是pandas timestamps
而不是python dates
:
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
添加到 apply
,但是使用一些熊猫函数是不可能的:
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:从年-月-日的列中获取日期的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!