【问题标题】:Count consecutive repeated values in pandas计算熊猫中的连续重复值
【发布时间】:2021-03-13 05:47:42
【问题描述】:

我试图在 Matplotlib 中突出显示 pandas 数据框中的数据在连续行数上相同的区域,因此给定下面的数据框和 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

最终目标是返回以下数据框,其中“结果”是一个测试,以查看“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'] 一起绘制。

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案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()
    

    【讨论】:

      【解决方案2】:

      通过使用移位测试连续相似性的布尔选择。应用 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
      

      【讨论】:

      • 谢谢,这也奏效了,pandas 中的 shift()、transform() 融化了我虚弱的大脑。
      猜你喜欢
      • 2019-12-07
      • 2019-03-04
      • 2023-03-10
      • 2014-12-09
      • 1970-01-01
      • 2013-10-28
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      相关资源
      最近更新 更多