如何解决如何找到由开始和结束列定义的时间间隔之间的总时间
我有一个熊猫数据帧:
我想通过以下方式计算确认和取消之间的差异:
对于日期 13.01.2020 和 desk_id 1.0:10:35:00 – 8:00:00 + 12:36:00 – 11:36:00 + 20:00:00 - 13:36:00
我只能在一个办公桌上执行这些操作,并且需要一小时的确认和取消。一小时我的意思是在desk_id的日期中,我只有一行用于确认和取消时间。当我从确认 8:00:00 和 20:00:00 中减去取消时间并将它们加在一起时,我得到了有趣的差异。
好几个小时,我无法把它放在一起。妈妈小时我的意思是在一个日期中的desk_id 有几行取消和确认时间。我想选择日期,desk_id 并计算办公桌占用时间 - 每个办公桌的确认和取消之间的差异。
输出应如下所示:
我想找出办公桌空闲的时间段。 在我的数据中可以在一个日期内多次确认和取消桌面。
我做了一小时确认和取消:
df_1['confirm'] = pd.to_timedelta(df_1['confirm'].astype(str))
df_1['diff_confirm'] = df_1['confirm'].apply(lambda x: x - datetime.timedelta(days=0,hours=8,minutes=0))
df_1['cancel'] = pd.to_timedelta(df_1['cancel'].astype(str))
df_1['diff_cancel'] = df_1['cancel'].apply(lambda x: datetime.timedelta(days=0,hours=20,minutes=0)-x)
这有效。
有什么建议吗?
解决方法
您没有完全清楚您需要什么格式的结果,但我认为将它们放在单独的数据框中是可以的。因此,此解决方案对由 date
和 desk_id
的值定义的每组行进行操作,并计算每组的总时间,并将输出放置在新的数据帧中:
用于创建输入数据框的代码:
from datetime import timedelta
import pandas as pd
df = pd.DataFrame(
{
'date': [pd.Timestamp('2020-1-13'),pd.Timestamp('2020-1-13'),pd.Timestamp('2020-1-14'),pd.Timestamp('2020-1-14')],'desk_id': [1.0,1.0,2.0,2.0],'confirm': ['10:36:00','12:36:00','09:36:00','10:36:00','15:36:00'],'cancel': ['11:36:00','13:36:00','11:36:00','14:36:00','16:36:00']
}
)
解决方案:
df['confirm'] = pd.to_timedelta(df['confirm'])
df['cancel'] = pd.to_timedelta(df['cancel'])
# function to compute total time each desk is free
def total_time(df):
return (
(df.iloc[0]['confirm'] - timedelta(days=0,hours=8,minutes=0)) +
(df['confirm'] - df['cancel'].shift()).sum() +
(timedelta(days=0,hours=20,minutes=0) - df.iloc[-1]['cancel'])
)
# apply function to each combination of 'desk_id' and 'date',producing
# a new dataframe
df.groupby(['desk_id','date']).apply(total_time).reset_index(name='total_time')
# desk_id date total_time
# 0 1.0 2020-01-13 0 days 10:00:00
# 1 1.0 2020-01-14 0 days 11:00:00
# 2 2.0 2020-01-13 0 days 10:00:00
# 3 2.0 2020-01-14 0 days 09:00:00
该函数取 confirm
的第一个值和 8:00:00 之间的差,取每个 confirm
和前面的 cancel
值之间的差,然后取 20 之间的差: 00:00 和 cancel
的最后一个值。将这些差异加在一起以产生最终值。
猜测您要做什么(我仍然无法完全理解,但可以尝试):
import pandas as pd
from datetime import timedelta as td
#create the dataframe
a = pd.DataFrame({'data':['2020-01-13','2020-01-13','2020-01-14'],'desk_id':[1.0,1.0],'confirm':['10:36:00','13:14:00'],'cancel':['11:36:00','13:44:00']})
def get_avail_times(df,start_end_delta=td(hours=12)):
df['confirm'] = pd.to_timedelta(df['confirm'])
df['cancel'] = pd.to_timedelta(df['cancel'])
#group by the two keys so that we can perform calculations on the specific groups!!
df_g = df.groupby(['data','desk_id'],as_index=False).sum()
df_g['total_time'] = start_end_delta - df_g['cancel'] + df_g['confirm']
return df_g.drop('confirm',1).drop('cancel',1)
output = get_avail_times(a)
给出输出:
data desk_id total_time
0 2020-01-13 1.0 0 days 10:00:00
1 2020-01-14 1.0 0 days 11:30:00
这里的关键是使用 .groupby() 函数,然后我们可以将其相加以基本上执行等式:
total_time = 20:00 + sum_confirm_times - sum_cancel_times - 08:00
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。