我有一个包含开始时间和结束时间的数据框。我想计算每行的时间差(以秒为单位),同时为该新计算添加新列。
dataframe = df
starttime endtime ID
1/7/2020 2:32:15 PM 1/7/2020 2:32:16 PM A
1/8/2020 2:33:20 PM 1/8/2020 2:33:22 PM B
我想要这个结果:
starttime endtime ID Diff
1/7/2020 2:32:15 PM 1/7/2020 2:32:16 PM A 1 sec
1/8/2020 2:33:20 PM 1/8/2020 2:33:22 PM B 2 sec
当前我有以下命令:
import numpy as np
import pandas as pd
import datetime
from dateutil.relativedelta import relativedelta
from datetime import date
df['Diff'] = df['endtime'] - df['starttime']
df['Diff']=df['Diff']/np.timedelta64(1,'s')
print(df)
答案 0 :(得分:2)
您可以执行以下操作。您将获得以秒为单位的差异(尽管您不会获得文本“ sec”)
df['Diff'] = (pd.to_datetime(df.endtime) - pd.to_datetime(df.starttime)).dt.total_seconds()
输出
starttime endtime ID Diff
0 1/7/2020 14:32:15 1/7/2020 14:32:16 A 1.0
1 1/8/2020 14:33:20 1/8/2020 14:33:22 B 2.0
答案 1 :(得分:1)
你去了
df['mydiff'] = pd.to_datetime(df.starttime) - pd.to_datetime(df.endtime)