Home > Blockchain >  Find the continuous interval and give an id
Find the continuous interval and give an id

Time:11-17

I would like to group each continuous region and to each region an id

For examplethe following data set

df = pd.Series(data = [0,1,1,1,2,2,2,16,17,18,18,18,18,1,1,1,1,2,2,200,201,3,4])

the output should be


pd.Series(data = [0,0,0,0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2,3,3,4,4])

I tried to use that code but it didn't work,

df= pd.concat([df, (df==0).cumsum()], axis = 1, keys = ['val', 'flag']> 

CodePudding user response:

IIUC you want to detect the points where the change is greater than 1. You can achieve that with a chain of simple arithmetic operations:

df.sub(df.shift(1)).abs().gt(1).cumsum()
  • Related