计算熊猫中连续重复的值

时间:2021-03-12 21:29:28

标签: python pandas matplotlib

我试图在 Matplotlib 中突出显示熊猫数据框中数据在连续行数上相同的区域,因此给定下面的数据框和 3 的阈值:

    days = pd.date_range(dt.datetime.now(), dt.datetime.now() + dt.timedelta(13), freq='D')
    data = [2,3,3,3,2,2,3.4,3.1,2.7,np.nan,4,4,4,4.5]
    df = pd.DataFrame({'cat': data})
    df = df.set_index(days)

出:

                            col
2021-03-12 15:13:24.727074  2.0
2021-03-13 15:13:24.727074  3.0
2021-03-14 15:13:24.727074  3.0
2021-03-15 15:13:24.727074  3.0
2021-03-16 15:13:24.727074  2.0
2021-03-17 15:13:24.727074  2.0
2021-03-18 15:13:24.727074  3.4
2021-03-19 15:13:24.727074  3.1
2021-03-20 15:13:24.727074  2.7
2021-03-21 15:13:24.727074  NaN
2021-03-22 15:13:24.727074  4.0
2021-03-23 15:13:24.727074  4.0
2021-03-24 15:13:24.727074  4.0
2021-03-25 15:13:24.727074  4.5

最终目标是返回以下数据帧,其中“result”是一个测试,以查看“col”中的数据是否没有变化。 2.0 的 2 个连续值不会标记,因为它们只是 2 个连续实例,而我们的阈值 >= 3。

                            col  result
2021-03-12 15:13:24.727074  2.0  False
2021-03-13 15:13:24.727074  3.0  True
2021-03-14 15:13:24.727074  3.0  True
2021-03-15 15:13:24.727074  3.0  True
2021-03-16 15:13:24.727074  2.0  False
2021-03-17 15:13:24.727074  2.0  False
2021-03-18 15:13:24.727074  3.4  False
2021-03-19 15:13:24.727074  3.1  False
2021-03-20 15:13:24.727074  2.7  False
2021-03-21 15:13:24.727074  NaN  False
2021-03-22 15:13:24.727074  4.0  True
2021-03-23 15:13:24.727074  4.0  True
2021-03-24 15:13:24.727074  4.0  True
2021-03-25 15:13:24.727074  4.5  False

我尝试使用下面的 cumsum() 并在有差异时增加 1。使用以下代码:

df['increment'] = (df['col'].diff(1) != 0).astype('int').cumsum()

这可以使用

获得连续块的大小
df.groupby('increment').size() >= threshold

这让我很接近,但问题是它破坏了我与原始数据帧日期时间索引的链接,这意味着我无法将布尔数据与原始 df['col'] 一起绘制。

2 个答案:

答案 0 :(得分:1)

在与 cumsum() 的比较中使用 shift 来识别块:

# groupby exact match of values
blocks = df['col'].ne(df['col'].shift()).cumsum()

df['result'] = blocks.groupby(blocks).transform('size') >= 3

输出:

                            col  result
2021-03-12 15:13:24.727074  2.0   False
2021-03-13 15:13:24.727074  3.0    True
2021-03-14 15:13:24.727074  3.0    True
2021-03-15 15:13:24.727074  3.0    True
2021-03-16 15:13:24.727074  2.0   False
2021-03-17 15:13:24.727074  2.0   False
2021-03-18 15:13:24.727074  3.4   False
2021-03-19 15:13:24.727074  3.1   False
2021-03-20 15:13:24.727074  2.7   False
2021-03-21 15:13:24.727074  NaN   False
2021-03-22 15:13:24.727074  4.0    True
2021-03-23 15:13:24.727074  4.0    True
2021-03-24 15:13:24.727074  4.0    True
2021-03-25 15:13:24.727074  4.5   False

注意 使用 == 比较浮点数并不理想。相反,我们可以使用阈值,例如:

# groupby consecutive rows if the differences are not significant
blocks = df['col'].diff().abs().gt(1e-6).cumsum()

答案 1 :(得分:1)

通过使用移位测试连续相似性来进行布尔选择。应用 cumsum 转换为组。使用生成的组进行分组。应用变换来查找大小。

  df=df.assign(result=df.groupby((~df.cat.eq(df.cat.shift())).cumsum())['cat'].transform('size').ge(3))

             

                            cat  result
2021-03-13 05:32:30.309303  2.0   False
2021-03-14 05:32:30.309303  3.0    True
2021-03-15 05:32:30.309303  3.0    True
2021-03-16 05:32:30.309303  3.0    True
2021-03-17 05:32:30.309303  2.0   False
2021-03-18 05:32:30.309303  2.0   False
2021-03-19 05:32:30.309303  3.4   False
2021-03-20 05:32:30.309303  3.1   False
2021-03-21 05:32:30.309303  2.7   False
2021-03-22 05:32:30.309303  NaN   False
2021-03-23 05:32:30.309303  4.0    True
2021-03-24 05:32:30.309303  4.0    True
2021-03-25 05:32:30.309303  4.0    True
2021-03-26 05:32:30.309303  4.5   False