【问题标题】:How to extract rows in Pandas based different criteria over two datetime columns如何根据两个日期时间列的不同条件在 Pandas 中提取行
【发布时间】:2021-07-30 10:46:59
【问题描述】:

我的目标是计算在同一时间段内同时工作的消防员人数。

我正在尝试从具有两列条件的数据框中提取行,但它没有按预期工作。

让我解释一下

这是我的数据(这是消防员干预的列表),其中包含消防员的数量以及干预的开始和结束。

ID Nombre d'agents (Engins) Date Début Sortie Engin Date Fin Sortie Engin
194683 3.0 2018-03-01 19:12:00 2018-03-01 19:54:00
194684 3.0 2018-03-01 19:20:00 2018-03-01 20:09:00
194685 3.0 2018-03-01 19:33:00 2018-03-01 20:16:00
194686 3.0 2018-03-01 19:50:00 2018-03-01 23:01:00
194687 3.0 2018-03-01 19:53:00 2018-03-01 20:20:00
194688 3.0 2018-03-01 19:54:00 2018-03-01 20:55:00
194689 3.0 2018-03-01 19:56:00 2018-03-01 21:20:00
194690 6.0 2018-03-01 20:03:00 2018-03-01 22:10:00
194691 3.0 2018-03-01 20:09:00 2018-03-01 20:54:00

这是我想要实现的目标: 在 2018 年 3 月 1 日 19:20:00 和 2018 年 3 月 1 日 19:54:00 之间:12 名消防员在同一时间工作,累计 1 小时 34 小时。 (第一行 19:20->19:54 (34mn) 中的 3 个,第二行 19:20->19:54 (34mn) 中的 3 个,第三行 19:33->19:54 (21) 中的 3 个,3 个从第四行 19:50->19:54 (4mn) 和 3 从第五行 19:53->19:54 (1mn))

我首先在数据框中按时间顺序组合所有日期时间(开始和结束),以便在行之间拥有所有时隙和时间增量。

data = [df["Date Début Sortie Engin"]]
headers = ["Moment"]
df3 = pd.concat(data, axis=1, keys=headers)
data = [df["Date Fin Sortie Engin"]]
df4 = pd.concat(data, axis=1, keys=headers)
df3 = df3.append(df4)
df3 = df3.sort_values(by="Moment", ascending=True)
Moment
2018-03-01 19:12:00
2018-03-01 19:20:00
2018-03-01 19:33:00
2018-03-01 19:50:00
2018-03-01 19:54:00

然后,我将这个新数据帧的两个连续行与我的初始数据进行比较,以了解有多少干预措施包括这个时间帧。我将消防员人数和同时发生的事件数相加

def calc_effectif(start, end):
    mask = (df['Date Début Sortie Engin'] >= start) & (df['Date Fin Sortie Engin'] <= end)
    return df['Nombre d\'agents (Engins)'].loc[mask].sum(), df['Nombre d\'agents (Engins)'].loc[mask].count()

df3["effectif"],df3["evenement"] = np.vectorize(calc_effectif)(df3["Moment"], df3["Moment"].shift(-1))

面具似乎不是这样做的正确方法。我已经研究了 pandas between_times 和其他函数,但它仅适用于索引......所以我现在有点卡住了。

关于如何在这方面取得进展的任何提示?

【问题讨论】:

  • 您的预期输出是什么?只需前两行就足够了。
  • 预期输出是一个包含消防员人数/花费时间/同时干预次数的数据透视表
  • 是的,我们都明白了。这个问题更多的是一个数字期望。
  • 谢谢,我实际上解决了我的问题,我的函数中的条件不正确。正确的条件是:mask = (df['Date Début Intervention (JJ/MM/AAAA HH:MM)']
  • Tu pourrais en fait poster ta solution pour 1) Ques nous puissions avoir un equivalent dans des situation differentes 2) afin de pouvoir obtenir des réputaions pour ton travail。 :-)

标签: python pandas datetime jupyter


【解决方案1】:

所以,我通过更改函数中的条件解决了我的问题: mask = (df['Début Sortie Engin'] &lt;= start) &amp; (end &lt;= df['Fin Sortie Engin'])

我最终得到了这样的结果:

Id Moment duree effectif evenement
0 01/03/2018 19:12 00:08:00 3.0 1
1 01/03/2018 19:20 00:13:00 6.0 2
2 01/03/2018 19:33 00:17:00 9.0 3
3 01/03/2018 19:50 00:03:00 12.0 4
4 01/03/2018 19:53 00:01:00 15.0 5
5 01/03/2018 19:54 00:00:00 18.0 6
0 01/03/2018 19:54 00:02:00 15.0 5
6 01/03/2018 19:56 00:07:00 18.0 6
7 01/03/2018 20:03 00:06:00 24.0 7
1 01/03/2018 20:09 00:00:00 27.0 8
8 01/03/2018 20:09 00:07:00 24.0 7
2 01/03/2018 20:16 00:04:00 21.0 6
4 01/03/2018 20:20 00:34:00 18.0 5
8 01/03/2018 20:54 00:01:00 15.0 4
5 01/03/2018 20:55 00:25:00 12.0 3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 2020-05-18
    • 2020-08-11
    相关资源
    最近更新 更多