【问题标题】:Check certain conditions looking back x hours (pandas)检查回顾 x 小时的某些条件(熊猫)
【发布时间】:2022-10-30 18:52:30
【问题描述】:

我有一些这样的数据:

import pandas as pd

dates = ["12/25/2021 07:47:01", "12/25/2021 08:02:32", "12/25/2021 13:57:40", "12/25/2021 14:17:11", "12/25/2021 17:23:01", "12/25/2021 23:48:55", "12/26/2021 08:22:32", "12/26/2021 11:11:11", "12/26/2021 14:53:40",  "12/26/2021 16:07:07", "12/26/2021 23:56:07"]
is_manual = [0,0,0,0,1,1,0,0,0,0,1]
is_problem = [0,0,0,0,1,1,0,0,0,1,1]

df = pd.DataFrame({'dates':dates,
                   'manual_entry': is_manual,
                   'problem_entry': is_problem})

    dates               manual_entry    problem_entry
0   12/25/2021 07:47:01 0                0
1   12/25/2021 08:02:32 0                0
2   12/25/2021 13:57:40 0                0
3   12/25/2021 14:17:11 0                0
4   12/25/2021 17:23:01 1                1
5   12/25/2021 23:48:55 1                1
6   12/26/2021 08:22:32 0                0
7   12/26/2021 11:11:11 0                0
8   12/26/2021 14:53:40 0                0
9   12/26/2021 16:07:07 0                1
10  12/26/2021 23:56:07 1                1

我想要做的是获取问题条目 == 1 的每一行并检查是否该行前 24 小时内的每一行是 manual_entry == 0

虽然我知道您可以创建一定数量的行的滚动回溯窗口,但每行之间没有正常的时间间隔,所以想知道如何回顾 24 小时并确定是否满足上述条件。

提前致谢

编辑:预期输出:

    dates               manual_entry    problem_entry
4   12/25/2021 17:23:01 1                1
10  12/26/2021 23:56:07 1                1

【问题讨论】:

  • 你的预期输出是什么?
  • 更新帖子,谢谢!

标签: python pandas


【解决方案1】:

尝试以下操作。将“manual_entry”提取到单独的变量中,并在当天的滑动窗口中收集金额。如果当前的“manual_entry”等于 1,则当天没有其他值。接下来,过滤数据帧,其中“problem_entry”、“manual_entry”等于 1。

df['dates'] = pd.to_datetime(df['dates'])
a = (df.rolling("86400s", on='dates', min_periods=1).sum()).loc[:, 'manual_entry']

print(df.loc[(df['problem_entry'] == 1) & (a == 1)])

输出:

                 dates  manual_entry  problem_entry
4  2021-12-25 17:23:01             1              1
10 2021-12-26 23:56:07             1              1

【讨论】:

    猜你喜欢
    • 2022-12-17
    • 2022-11-10
    • 2022-12-11
    • 1970-01-01
    • 2014-09-05
    • 2020-05-16
    • 2023-03-11
    • 2022-11-28
    • 1970-01-01
    相关资源
    最近更新 更多