[英]Python pandas: create a dataframe of dates from existing columns
I have a pandas dataframe with a column for years and one for months. 我有一个pandas数据框,其中有一个列数年,一个列数月。 How can I create a new date column based on these two (I can assume day = 15). 如何基于这两个创建新的日期列(我可以假设day = 15)。
I tried the following: 我尝试了以下方法:
import pandas as pd
import numpy as np
import datetime
df = pd.DataFrame()
df['year'] = np.arange(2000,2010)
df['mydate']= datetime.date( df['year'].apply(lambda x: int(x)) , 1 , 1)
but I get this error message: 但我收到此错误消息:
df['mydate']= datetime.date( df['year'].apply(lambda x: int(x)) , 1 , 1) File "C:\\Anaconda\\lib\\site-packages\\pandas\\core\\series.py",
line 77, in wrapper "cannot convert the series to {0}".format(str(converter))) TypeError: cannot convert the series to 第77行,在包装器中“无法将系列转换为{0}”。format(str(converter)))TypeError:无法将系列转换为
which I don't understand because I explictly convert x to int. 我不明白,因为我明确将x转换为int。
Thanks! 谢谢!
You can build another column based on the existing columns by using df.apply(fnc, axis=1)
. 您可以使用df.apply(fnc, axis=1)
在现有列的基础上构建另一列。
In your case this becomes: 在您的情况下,它将变为:
df = pd.DataFrame()
df['year'] = np.arange(2000,2010)
df['month'] = 6
df['date_time']= df.apply(lambda row :
datetime.date(row.year,row.month,15),
axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.