我有一个如下的数据集,每个ID可以在任何给定的时间和持续时间内签入和签出
Product product = _uow.Products.SelectProduct(id);
if (product == null)
{
return RedirectToPage("Productslist");
}
else
{
return View("ProductPage", productVm);
}
从此计算出的“签入分钟数”需要分为下表所示的小时时段,以便即使在整个签入结帐时,我也可以按小时和天数按每个ID的小时数来计算累计总数天。
帮助表示感谢:)
ID checkin_datetime checkout_datetime
4 04-01-2019 13:07 04-01-2019 13:09
4 04-01-2019 13:09 04-01-2019 13:12
4 04-01-2019 14:06 04-01-2019 14:07
4 04-01-2019 14:55 04-01-2019 15:06
22 04-01-2019 20:23 04-01-2019 21:32
22 04-01-2019 21:38 04-01-2019 21:42
25 04-01-2019 23:22 04-02-2019 00:23
29 04-02-2019 01:00 04-02-2019 06:15
创建数据框的代码:
ID checkin_datetime checkout_datetime day HR Minutes
4 04-01-2019 13:07 04-01-2019 13:09 04-01-2019 13 2
4 04-01-2019 13:09 04-01-2019 13:12 04-01-2019 13 3
4 04-01-2019 14:06 04-01-2019 14:07 04-01-2019 14 1
4 04-01-2019 14:55 04-01-2019 15:06 04-01-2019 14 5
4 04-01-2019 14:55 04-01-2019 15:06 04-01-2019 15 6
22 04-01-2019 20:23 04-01-2019 21:32 04-01-2019 20 27
22 04-01-2019 20:23 04-01-2019 21:32 04-01-2019 21 32
22 04-01-2019 21:38 04-01-2019 21:42 04-01-2019 21 4
25 04-01-2019 23:22 04-02-2019 00:23 04-01-2019 23 28
25 04-01-2019 23:22 04-02-2019 00:23 04-02-2019 0 23
29 04-02-2019 01:00 04-02-2019 06:15 04-02-2019 1 60
29 04-02-2019 01:00 04-02-2019 06:15 04-02-2019 2 60
29 04-02-2019 01:00 04-02-2019 06:15 04-02-2019 3 60
29 04-02-2019 01:00 04-02-2019 06:15 04-02-2019 4 60
29 04-02-2019 01:00 04-02-2019 06:15 04-02-2019 5 60
29 04-02-2019 01:00 04-02-2019 06:15 04-02-2019 6 15
答案 0 :(得分:0)
非常简单:
-在此期间,您只需从签入中减去签出即可(datetime
可以做到)。
-要在几分钟内得到它-用一分钟的timedelta
除以一分钟(我将使用内置的pandas
)。
-要从datetime
中获取小时,请调用.hour
,然后类似地.date()
来获取日期(第一个是属性,第二个是方法-注意括号)。>
df['Hour'] = df['checkin_datetime'].apply(lambda x: x.hour)
df['Date'] = df['checkin_datetime'].apply(lambda x: x.date())
df['duration'] = df['checkout_datetime']-df['checkin_datetime']
df['duration_in_minutes'] = (df['checkout_datetime']-df['checkin_datetime'])/pd.Timedelta(minutes=1)
[编辑]:我有一个解决方案,可以将持续时间分成几个小时,但这不是最优雅的方法。
df2 = pd.DataFrame(
index=pd.DatetimeIndex(
start=df['checkin_datetime'].min(),
end=df['checkout_datetime'].max(),freq='1T'),
columns = ['is_checked_in','ID'], data=0)
for index, row in df.iterrows():
df2['is_checked_in'][row['checkin_datetime']:row['checkout_datetime']] = 1
df2['ID'][row['checkin_datetime']:row['checkout_datetime']] = row['ID']
df3 = df2.resample('1H').aggregate({'is_checked_in': sum,'ID':max})
df3['Hour'] = df3.index.to_series().apply(lambda x: x.hour)
答案 1 :(得分:0)
import pandas as pd
data={'ID':[4,4,4,4,22,22,25,29],
'checkin_datetime':['04-01-2019 13:07','04-01-2019 13:09','04-01-2019 14:06','04-01-2019 14:55','04-01-2019 20:23'
,'04-01-2019 21:38','04-01-2019 23:22','04-02-2019 01:00'],
'checkout_datetime':['04-01-2019 13:09','04-01-2019 13:12','04-01-2019 14:07','04-01-2019 15:06','04-01-2019 21:32'
,'04-01-2019 21:42','04-02-2019 00:23'
,'04-02-2019 06:15']
}
df = pd.DataFrame(data,columns= ['ID', 'checkin_datetime','checkout_datetime'])
df['checkout_datetime'] = pd.to_datetime(df['checkout_datetime'])
df['checkin_datetime'] = pd.to_datetime(df['checkin_datetime'])
df['Hour'] = df['checkin_datetime'].apply(lambda x: x.hour)
df['Date'] = df['checkin_datetime'].apply(lambda x: x.date())
df['duration'] = df['checkout_datetime']-df['checkin_datetime']
df['duration_in_minutes'] = (df['checkout_datetime']-df['checkin_datetime'])/pd.Timedelta(minutes=1)
with pd.option_context('display.max_rows', None, 'display.max_columns', None): # more options can be specified also
print(df)
我认为Itamar Muskhkin先前给出的答案是绝对正确的。