计数两个定时之间的表中的行数

发布于 2025-02-12 00:59:22 字数 1380 浏览 1 评论 0原文

我有这张桌子:

 IdNum                  BeginDate                      Exitdate
-------------------------------------------------------------------------
123                    2022-06-13 09:03               2022-06-13 22:12
633                    2022-06-13 08:15               2022-06-13 13:09
389                    2022-06-13 10:03               2022-06-13 18:12
665                    2022-06-13 08:30               2022-06-13 10:12

我想在一天的每15分钟中获取数据,以了解该人(IDNUM)是否在此过程中。

例如:我想知道在18:00到18:15之间的过程中有多少个IDNUM。

根据上表,我应该得到:2

,因为在一天的15分钟内,只有IDNUM 123和IDNUM 389。

我可以编写一个函数或可以在上面接收该表的功能,然后返回一个带有96列的新表(对于一天的每15分钟),对于每行(IDNUM)时间?

示例:

 IdNum      BeginDate               Exitdate                00:00 - 00:15       18:00 - 18:15      etc etc..
----------------------------------------------------------------------------------------------
123         2022-06-13 09:03        2022-06-13 22:12         FALSE              TRUE
633         2022-06-13 08:15        2022-06-13 13:09         FALSE              FALSE
389         2022-06-13 10:03        2022-06-13 18:12         FALSE              TRUE
665         2022-06-13 08:30        2022-06-13 10:12         FALSE              FALSE

接收3个变量的函数更容易

或者也许编写一个 在那段时间之间)。

在Python上更容易做什么?有可能吗?您能帮我牵头还是在Python上做些更好的方法?我上面提出的第一个还是第二个?

I've got this table:

 IdNum                  BeginDate                      Exitdate
-------------------------------------------------------------------------
123                    2022-06-13 09:03               2022-06-13 22:12
633                    2022-06-13 08:15               2022-06-13 13:09
389                    2022-06-13 10:03               2022-06-13 18:12
665                    2022-06-13 08:30               2022-06-13 10:12

I want to get data for each 15 min of the day to know if the person (IdNum) was in the process.

For example: I want to know how many IdNum were in the process between 18:00 and 18:15.

According to the table above I should get: 2

Because only IdNum 123 and IdNum 389 were in the process during that 15 minutes of the day.

Can I write a function or something that receive that table above and returns a new table with 96 new column(for each 15 min of the day) and for each row (IdNum) you get True of False if he were in the process during that time?

Example:

 IdNum      BeginDate               Exitdate                00:00 - 00:15       18:00 - 18:15      etc etc..
----------------------------------------------------------------------------------------------
123         2022-06-13 09:03        2022-06-13 22:12         FALSE              TRUE
633         2022-06-13 08:15        2022-06-13 13:09         FALSE              FALSE
389         2022-06-13 10:03        2022-06-13 18:12         FALSE              TRUE
665         2022-06-13 08:30        2022-06-13 10:12         FALSE              FALSE

OR maybe it is easier to write a function that receives 3 variables:

a table, a start time (exmp: 18:00), end time (exmp: 18:15) and it returns: 2 (number of IdNum's that were present between those times).

what is easier to do on Python? is that possible? can you help me with a lead or something of what is the better way to do it on Python? The first one or the second one I presented above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜你已久 2025-02-19 00:59:22

在我的解决方案,您将在员工在第一个示例中显示的员工在这里的时间内拥有一个具有ID和布尔值的数据框。

import pandas as pd

#Your initial Dataframe
df = pd.DataFrame([[123,"2022-06-13 09:03", "2022-06-13 22:12"],[633, "2022-06-13 08:15", "2022-06-13 13:09"]], columns=['IdNum', 'BeginDate', 'Exitdate'])

#The dictionnary where I'll stock the result
dico_res = {}
for i in range(df.shape[0]):

  #I define a range of dates to know if your enter and exit is in the range
  enter = pd.to_datetime(df.loc[i]["BeginDate"])
  exit = pd.to_datetime(df.loc[i]["Exitdate"])
  start = pd.to_datetime(enter.strftime("%d/%m/%Y"))
  range_15_minutes = pd.date_range(start=start, end=end,freq="15min")
  list_boolean, idx = [], []
  
  for date in range(len(range_15_minutes)-1):
    if enter >= range_15_minutes[date] and enter < range_15_minutes[date+1]:
      list_boolean.append(True)
      
    elif exit >= range_15_minutes[date] and exit < range_15_minutes[date+1]:
      list_boolean.append(True)
    
    elif exit < range_15_minutes[date] or enter > range_15_minutes[date]:
        list_boolean.append(False)
    
    else:
      list_boolean.append(True)
      
    idx.append(range_15_minutes[date].strftime("%H:%M") + "-" + range_15_minutes[date+1].strftime("%H:%M"))

  dico_res[df.loc[i]["IdNum"]]=list_boolean

dataframe_solution = pd.DataFrame(dico_res, index=idx).T

Here my solution, you will have a Dataframe with Id as idex and booleans for the hours where the employees were here like you've shown in your first example.

import pandas as pd

#Your initial Dataframe
df = pd.DataFrame([[123,"2022-06-13 09:03", "2022-06-13 22:12"],[633, "2022-06-13 08:15", "2022-06-13 13:09"]], columns=['IdNum', 'BeginDate', 'Exitdate'])

#The dictionnary where I'll stock the result
dico_res = {}
for i in range(df.shape[0]):

  #I define a range of dates to know if your enter and exit is in the range
  enter = pd.to_datetime(df.loc[i]["BeginDate"])
  exit = pd.to_datetime(df.loc[i]["Exitdate"])
  start = pd.to_datetime(enter.strftime("%d/%m/%Y"))
  range_15_minutes = pd.date_range(start=start, end=end,freq="15min")
  list_boolean, idx = [], []
  
  for date in range(len(range_15_minutes)-1):
    if enter >= range_15_minutes[date] and enter < range_15_minutes[date+1]:
      list_boolean.append(True)
      
    elif exit >= range_15_minutes[date] and exit < range_15_minutes[date+1]:
      list_boolean.append(True)
    
    elif exit < range_15_minutes[date] or enter > range_15_minutes[date]:
        list_boolean.append(False)
    
    else:
      list_boolean.append(True)
      
    idx.append(range_15_minutes[date].strftime("%H:%M") + "-" + range_15_minutes[date+1].strftime("%H:%M"))

  dico_res[df.loc[i]["IdNum"]]=list_boolean

dataframe_solution = pd.DataFrame(dico_res, index=idx).T
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文