我有一个数据框,其中最后两列指的是时钟时间。两列的数据类型均为String。
样本数据如下:
PROCESS_N VAL DATE TIME_1 TIME_2
35324399 74.95 02/11/18 12:45:26 12:30:36
35324399 74.95 02/11/18 12:45:26 12:35:36
35334154 74.95 02/11/18 12:45:27 12:36:22
35338698 74.95 02/11/18 12:45:28 12:38:30
35338698 74.95 02/11/18 12:45:28 12:38:32
35347905 74.95 02/11/18 12:45:30 12:39:36
35367939 74.95 02/11/18 12:45:30 12:39:39
35371892 74.95 02/11/18 12:45:31 12:39:55
我想在Dataframe的末尾填充另一列,其中TIME_1和TIME_2的差异为Second。
答案 0 :(得分:2)
您可以使用pd.to_timedelta
,然后访问pd.Series.dt.seconds
:
time_cols = ['TIME_1', 'TIME_2']
df[time_cols] = df[time_cols].apply(pd.to_timedelta)
df['Diff'] = (df['TIME_1'] - df['TIME_2']).dt.seconds
print(df)
PROCESS_N VAL DATE TIME_1 TIME_2 Diff
0 35324399 74.95 02/11/18 12:45:26 12:30:36 890
1 35324399 74.95 02/11/18 12:45:26 12:35:36 590
2 35334154 74.95 02/11/18 12:45:27 12:36:22 545
3 35338698 74.95 02/11/18 12:45:28 12:38:30 418
4 35338698 74.95 02/11/18 12:45:28 12:38:32 416
5 35347905 74.95 02/11/18 12:45:30 12:39:36 354
6 35367939 74.95 02/11/18 12:45:30 12:39:39 351
7 35371892 74.95 02/11/18 12:45:31 12:39:55 336
答案 1 :(得分:1)
您可以简单地使用to_datetime()
方法进行转换,然后进行区别:
df['diff'] = pd.to_datetime(df['TIME_1']) - pd.to_datetime(df['TIME_2'])
如果您希望将其存储在几秒钟内,则可以这样转换:
df['diff'] = df['diff'].astype('timedelta64[s]')
输出:
PROCESS_N VAL DATE TIME_1 TIME_2 diff
0 35324399 74.95 02/11/18 12:45:26 12:30:36 890.0
1 35324399 74.95 02/11/18 12:45:26 12:35:36 590.0
2 35334154 74.95 02/11/18 12:45:27 12:36:22 545.0
3 35338698 74.95 02/11/18 12:45:28 12:38:30 418.0
4 35338698 74.95 02/11/18 12:45:28 12:38:32 416.0
5 35347905 74.95 02/11/18 12:45:30 12:39:36 354.0
6 35367939 74.95 02/11/18 12:45:30 12:39:39 351.0
7 35371892 74.95 02/11/18 12:45:31 12:39:55 336.0
答案 2 :(得分:1)
使用timedelta
-
df['Diff'] = (df['TIME_1']-df['TIME_2']).astype('timedelta64[s]')
输出
PROCESS_N VAL DATE TIME_1 TIME_2 Diff
0 35324399 74.95 02/11/18 1900-01-01 12:45:26 1900-01-01 12:30:36 890.0
1 35324399 74.95 02/11/18 1900-01-01 12:45:26 1900-01-01 12:35:36 590.0
2 35334154 74.95 02/11/18 1900-01-01 12:45:27 1900-01-01 12:36:22 545.0
3 35338698 74.95 02/11/18 1900-01-01 12:45:28 1900-01-01 12:38:30 418.0
4 35338698 74.95 02/11/18 1900-01-01 12:45:28 1900-01-01 12:38:32 416.0
5 35347905 74.95 02/11/18 1900-01-01 12:45:30 1900-01-01 12:39:36 354.0
6 35367939 74.95 02/11/18 1900-01-01 12:45:30 1900-01-01 12:39:39 351.0
7 35371892 74.95 02/11/18 1900-01-01 12:45:31 1900-01-01 12:39:55 336.0
如果有str
-
df['TIME_1'] = pd.to_datetime(df['TIME_1'],format='%H:%M:%S')
df['TIME_2'] = pd.to_datetime(df['TIME_2'],format='%H:%M:%S')
答案 3 :(得分:1)
如果您知道如何使用数据框,则此代码将为您提供良好的帮助:
import datetime
import time
x1 = time.strptime('12:45:26,000'.split(',')[0], '%H:%M:%S')
x2 = time.strptime('12:30:36,000'.split(',')[0], '%H:%M:%S')
delta_x = datetime.timedelta(hours=x1.tm_hour - x2.tm_hour, minutes=x1.tm_min -
x2.tm_min, seconds=x1.tm_sec - x2.tm_sec).total_seconds()
print(delta_x)
答案 4 :(得分:0)
看看将两个数字都收敛到datetime objects,然后将它们相减,就像这样:
import datetime as dt
time1 = dt.datetime(2018,12,30,23,59,59)
time2 = dt.datetime(2018,12,31,23,59,59)
(time2-time1).total_seconds()
(source)