Python 如何在两列时间对象之间建立差异?
我有一个数据帧Python 如何在两列时间对象之间建立差异?,python,pandas,datetime,Python,Pandas,Datetime,我有一个数据帧df,其中包含一天中请求时间的第一个(min)和最后一个(max),我希望建立差异,以便获得每天的工作时数 min max dates 2006-09-11 10:58:50 23:02:05 2006-09-12 00:59:26 19:06:29 2006-09-13 09:10:32 21:50:56 2006-09-14 10:25:43 22:40:13 200
df
,其中包含一天中请求时间的第一个(min
)和最后一个(max
),我希望建立差异,以便获得每天的工作时数
min max
dates
2006-09-11 10:58:50 23:02:05
2006-09-12 00:59:26 19:06:29
2006-09-13 09:10:32 21:50:56
2006-09-14 10:25:43 22:40:13
2006-09-18 10:43:29 21:00:54
2006-09-19 08:21:37 21:05:41
2006-09-20 08:10:12 22:50:45
2006-09-21 10:29:40 22:16:23
2006-09-25 06:56:57 20:19:48
2006-09-26 00:42:25 21:31:07
2006-09-27 01:10:49 22:20:49
2006-09-28 09:55:26 20:58:46
2006-10-02 10:38:39 23:08:32
2006-10-03 09:44:46 23:20:40
2006-10-04 10:34:04 23:51:21
2006-10-05 10:04:50 23:43:43
2006-10-09 10:01:59 21:14:20
2006-10-10 09:58:45 18:48:06
2006-10-11 09:39:44 19:12:39
2006-10-12 02:34:16 23:08:57
2006-10-16 09:44:53 20:37:10
2006-10-17 10:24:33 19:09:37
2006-10-18 00:18:08 22:19:42
我想有一个新的专栏df['wh']
,在这里我可以看到两次之间的差异。
我发现没有一个建议对我有帮助。。谢谢你的帮助 我们必须将您的时间字符串转换为时间格式,以便差分函数在您的情况下工作。之后,我们只需将类型改回string 我相信下面的代码可能适合您
import pandas as pd
df['min_as_time_format'] = pd.to_datetime(df['min'], format="%H:%M:%S")
df['max_as_time_format'] = pd.to_datetime(df['max'], format="%H:%M:%S")
df['diff'] = df['max_as_time_format'] - df['min_as_time_format']
df['diff'] = df['diff'].astype(str).str[-18:-10]
让我知道它是否适用于您,但我甚至不能减去
df.max df.min
。我得到了错误:不支持的操作数类型-:“method”和“method”的类型是max
和min
这正是我想要的。谢谢谢谢,请接受并投票回答:)