[英]Count consecutive repeated values in pandas
我試圖突出 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
最終目標是返回以下 dataframe,其中“結果”是測試“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
這讓我很接近,但問題是它破壞了我與原始 dataframe 日期時間索引的鏈接,這意味着我不能 plot boolean 數據與原始數據'col'一起。
在與shift
進行比較時使用cumsum()
來識別塊:
# groupby exact match of values
blocks = df['col'].ne(df['col'].shift()).cumsum()
df['result'] = blocks.groupby(blocks).transform('size') >= 3
Output:
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()
Boolean select 通過使用移位測試連續相似性。 應用 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.