Identify consecutive same values in Pandas Dataframe, with a Groupby

后端 未结 4 1678
说谎
说谎 2020-12-08 05:18

I have the following dataframe df:

data={\'id\':[1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2],
      \'value\':[2,2,3,2,2,2,3,3,3,3,1,4,1,1,1,4,4,1,1,1,1,1]}         


        
4条回答
  •  星月不相逢
    2020-12-08 06:08

    #try this simpler version
    a= pd.Series([1,1,1,2,3,4,5,5,5,7,8,0,0,0])
    b= a.groupby([a.ne(0), a]).transform('size').ge(3).astype('int')
    #ge(x) <- x is the number of consecutive repeated values 
    print b
    

提交回复
热议问题