Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对熊猫中的连续重复值进行计数_Python_Pandas_Matplotlib - Fatal编程技术网

Python 对熊猫中的连续重复值进行计数

Python 对熊猫中的连续重复值进行计数,python,pandas,matplotlib,Python,Pandas,Matplotlib,我试图突出显示Matplotlib中的区域,其中pandas数据框中的数据在连续数行上是相同的,因此给定下面的数据框和阈值3: 在 输出: 最终目标是返回以下数据帧,其中“result”是一个测试,以查看“col”中的数据是否没有变化。2.0的两个连续值没有标记,因为与我们的阈值>=3相比,它们只有两个连续实例 col result 2021-03-12 15:13:24.727074 2.0 False 2021-03-13 15:

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

输出:

最终目标是返回以下数据帧,其中“result”是一个测试,以查看“col”中的数据是否没有变化。2.0的两个连续值没有标记,因为与我们的阈值>=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
这让我很接近,但问题是它破坏了我与原始dataframe日期时间索引的链接,这意味着我无法将布尔数据与原始df['col']一起绘制。

使用
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()

通过使用shift测试连续相似性进行布尔选择。应用cumsum以转换为组。将生成的组用于groupby。应用变换查找大小

  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

谢谢,这也起作用了,在熊猫身上的shift(),transform()融化了我虚弱的大脑。
# groupby consecutive rows if the differences are not significant
blocks = df['col'].diff().abs().gt(1e-6).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