【问题标题】:python Pandas - add/insert rows before or after specific sequence of column values and count number of imputed rowspython Pandas - 在特定列值序列之前或之后添加/插入行并计算推算行数
【发布时间】:2021-01-21 02:08:08
【问题描述】:

我有一个包含设备信息的大型数据框,按设备名称和时间顺序排列。

data = [['abc01', 3000.0, 'transac_complete', 'system', '13:10:37', 1],
       ['abc01', 100.0, 'start', 'system', '13:12:17', 2],
       ['abc01', 500.0, 'stop', 'system', '13:20:37', 3],
       ['abc01', 0.3, 'a_type_1', 'a', '13:20:37', 3],
       ['abc01', 0.2, 'a_type_2', 'a', '13:20:38', 3],
       ['abc01', 200.0, 'start', 'system', '13:23:58', 3],
       ['abc01', 1120.0, 'transac_complete', 'system', '13:42:37', 3],
       ['abc01', 505.0, 'transac_complete', 'system', '13:51:02', 3],
       ['abc01', 600.0, 'stop', 'system', '14:01:02', 4],
       ['abc01', 0.233, 'a_type_3', 'a', '14:01:03', 4],
       ['abc01', 0.244, 'b_type_1', 'b', '14:01:03', 4],
       ['abc01', 5.0, 'b_type_2', 'b', '14:01:08', 4],
       ['abc01', 0.33299999999999996, 'c_type_1', 'c', '14:01:08', 4],
       ['abc01', 1500.0, 'start', 'system', '14:26:08', 4],
       ['abc01', 24.0, 'stop', 'system', '14:26:32', 5],
       ['abc01', 500.0, 'start', 'system', '14:34:52', 5],
       ['abc01', 1000.0, 'stop', 'system', '14:51:32', 6],
       ['abc02', 9009.0, 'transac_complete', 'system', '17:21:41', 6],
       ['abc02', 66.0, 'stop', 'system', '17:22:47', 6],
       ['abc02', 100.0, 'stop', 'system', '17:24:27', 6],
       ['abc02', 599.0, 'start', 'system', '17:34:26', 6]]

columns = ['name', 'duration_sec', 'event', 'err_grp', 'timestamp', 'sequence']

df = pd.DataFrame.from_records(data, columns=columns)

我的数据集的示例部分如下所示:

name duration_sec event err_grp timestamp sequence
1 abc01 3000 transac_complete system 13:10:37 1
2 abc01 100 start system 13:12:17 2
3 abc01 500 stop system 13:20:37 3
4 abc01 0.3 a_type_1 a 13:20:37 3
5 abc01 0.2 a_type_2 a 13:20:38 3
6 abc01 200 start system 13:23:58 3
7 abc01 1120 transac_complete system 13:42:37 3
8 abc01 505 transac_complete system 13:51:02 3
9 abc01 600 stop system 14:01:02 4
10 abc01 0.233 a_type_3 a 14:01:03 4
11 abc01 0.244 b_type_1 b 14:01:03 4
12 abc01 5 b_type_2 b 14:01:08 4
13 abc01 0.333 c_type_1 c 14:01:08 4
14 abc01 1500 start system 14:26:08 4
15 abc01 24 stop system 14:26:32 5
16 abc01 500 start system 14:34:52 5
17 abc01 1000 stop system 14:51:32 6
18 abc02 9009 transac_complete system 17:21:41 6
19 abc02 66 stop system 17:22:47 6
20 abc02 100 stop system 17:24:27 6
21 abc02 599 start system 17:34:26 6

对此,我意识到缺少“开始”值,并且我的前 2 个值不符合条件,因为事件序列是单行事件,这不是我想要的。

  1. 如何添加/插入满足两个“停止”值(即索引 19-20)之间没有“开始”值的条件的新行,无论它们是否连续。

对于连续值,这个新的“开始”行将采用之前“停止”行的时间戳值,并且持续时间值基于“停止”值之前和之后的时间戳差异,应该是零。

对于非连续值(即,在 2 个“停止”值之间存在不是“开始”的值),新行应位于第一个“停止”行之后,持续时间和时间值如上所述。

缩进df:

name duration_sec event err_grp timestamp sequence
1 abc01 3000 transac_complete system 13:10:37
2 abc01 100 start system 13:12:17
3 abc01 500 stop system 13:20:37 1
4 abc01 0.3 a_type_1 a 13:20:37 1
5 abc01 0.2 a_type_2 a 13:20:38 1
6 abc01 200 start system 13:23:58 1
7 abc01 1120 transac_complete system 13:42:37
8 abc01 505 transac_complete system 13:51:02
9 abc01 600 stop system 14:01:02 2
10 abc01 0.233 a_type_3 a 14:01:03 2
11 abc01 0.244 b_type_1 b 14:01:03 2
12 abc01 5 b_type_2 b 14:01:08 2
13 abc01 0.333 c_type_1 c 14:01:08 2
14 abc01 1500 start system 14:26:08 2
15 abc01 24 stop system 14:26:32
16 abc01 500 start system 14:34:52
17 abc01 1000 stop system 14:51:32
18 abc02 9009 transac_complete system 17:21:41
19 abc02 66 stop system 17:22:47
20 abc02 0 start system 17:22:47
21 abc02 100 stop system 17:24:27
22 abc02 599 start system 17:34:26

应根据仅包含具有“停止-事件XXX(一个或多个事件)-开始”的模式来更改顺序,并排除背靠背“停止-开始”发生的次数。

  1. 我如何计算缺失的“开始”值的数量,或估算的“开始”值的数量,因为它们是相同的。

感谢任何帮助。谢谢!

【问题讨论】:

    标签: python pandas pattern-matching sequence


    【解决方案1】:

    解决方案可能是在导入到 pandas 之前遍历列表项以检查两个连续的停止值。一个简单的计数器将回答您的第二个问题:

    missing_start_values = 0
    for num, i in enumerate(data):
        if 'stop' in data[num-1] and 'stop' in i:
            data.insert(num, [data[num-1][0], 0, 'start', data[num-1][3], data[num-1][4]])
            missing_start_values += 1
    print(missing_start_values)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 2017-10-22
      • 1970-01-01
      相关资源
      最近更新 更多