[英]Adding two time columns together in pandas dataframe?
I have the following data: 我有以下数据:
time_begin DRTN_IN_SCND
16:22:16 439
16:29:37 53
16:30:33 85
I would like to make a new column that adds time_begin and DRTN_IN_SCND (the duration in seconds) to create a new time. 我想创建一个新列,添加time_begin和DRTN_IN_SCND(以秒为单位的持续时间)来创建新时间。
I have tried: 我试过了:
df['new_time'] = df['time_begin'].apply(lambda x: (dt.datetime.combine(dt.datetime(1,1,1), x,) + dt.timedelta(seconds=df.DRTN_IN_SCND)).time())
This works if dt.timedelta(seconds=3) but does not work when I change to dt.timedelta(seconds=df.DRTN_IN_SCND). 这适用于dt.timedelta(seconds = 3)但在我更改为dt.timedelta(seconds = df.DRTN_IN_SCND)时不起作用。 I get the following error. 我收到以下错误。
TypeError: unsupported type for timedelta seconds component: Series
Does anyone know how to fix this or of another way to accomplish what I'm trying to do? 有谁知道如何解决这个或其他方式来完成我想要做的事情? Thanks! 谢谢!
You'll have to convert the DRTN_IN_SCND
and time_begin
to time deltas if you want to do properly calculations on the columns, pandas has to_timedelta which is pretty handy: 如果你想对列进行正确的计算,你必须将DRTN_IN_SCND
和time_begin
转换为时间增量,pandas具有非常方便的to_timedelta :
df['DRTN_IN_SCND'] = pd.to_timedelta(df['DRTN_IN_SCND'], unit='s')
df['time_begin'] = pd.to_timedelta(df['time_begin'])
df['new_time'] = df['time_begin'] + df['DRTN_IN_SCND']
This will give you the new column new_time
: 这将为您提供新列new_time
:
time_begin DRTN_IN_SCND new_time
0 16:22:16 00:07:19 16:29:35
1 16:29:37 00:00:53 16:30:30
2 16:30:33 00:01:25 16:31:58
You are using apply
on df['new_time']
which is a series and in the lambda
you are referring to df.DRTN_IN_SCND
which is another series. 您正在使用df['new_time']
上的apply
,这是一个系列,在lambda
你指的是df.DRTN_IN_SCND
,这是另一个系列。 So the error states that you are trying to add a time object to a series object and it doesn't know what to do. 因此,错误表明您正在尝试将时间对象添加到系列对象,并且它不知道该怎么做。
Instead, use apply
on the dataframe. 相反,请在数据框上使用apply
。 In this context, the x
's in the lambda function are series and you can access each component via ix
. 在这种情况下,lambda函数中的x
是串联的,您可以通过ix
访问每个组件。 This does what you want. 这样做你想要的。
df['new_time'] = df.apply(lambda x: x.ix['time_begin'] + dt.timedelta(seconds=x.ix['DRTN_IN_SCND']), axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.