我有以下数据:
time_begin DRTN_IN_SCND 16:22:16 439 16:29:37 53 16:30:33 85我想创建一个新列,其中添加time_begin和DRTN_IN_SCND(以秒为单位的持续时间)以创建新时间.
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.
我尝试过:
df['new_time'] = df['time_begin'].apply(lambda x: (dt.datetimebine(dt.datetime(1,1,1), x,) + dt.timedelta(seconds=df.DRTN_IN_SCND)).time())如果dt.timedelta(seconds = 3)有效,但是当我更改为dt.timedelta(seconds = df.DRTN_IN_SCND)时不起作用.我收到以下错误.
This works if dt.timedelta(seconds=3) but does not work when I change to 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!
推荐答案如果要对列进行正确的计算,则必须将DRTN_IN_SCND和time_begin转换为时间增量,pandas具有 to_timedelta 十分方便:
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:
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']这将为您提供新的列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更多推荐
在 pandas 数据框中将两个时间列加在一起?
发布评论