I have an Excel file with the following columns that I read into a pandas dataframe:
Year Month Day Hour
2012 1 1 0
2012 1 1 1
2012 1 1 2
2012 1 1 3`
I'm trying to combine these 3 columns into a datetime column using the following code:
df1 = df.assign(Dt = lambda row: datetime.datetime(row['Year'].astype(int), row['Month'].astype(int)...))
This code gives the following error: Type Error: Cannot convert the series to (Type 'int')
I know I can just combine these columns into a string and use strptime
to convert to datetime
. HoweverI i want to try to understand what i'm doing wrong with assign and lambda here.
All your columns are appropriately named to use pd.to_datetime
directly on the df
df.assign(Dt=pd.to_datetime(df))
Year Month Day Hour Dt
0 2012 1 1 0 2012-01-01 00:00:00
1 2012 1 1 1 2012-01-01 01:00:00
2 2012 1 1 2 2012-01-01 02:00:00
3 2012 1 1 3 2012-01-01 03:00:00
which simplifies to:
df.assign(Dt=pd.to_datetime)
The passed callable is not called once per row as you think.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.