Finding consecutive segments in a pandas data frame

前端 未结 2 530
有刺的猬
有刺的猬 2020-11-28 04:48

I have a pandas.DataFrame with measurements taken at consecutive points in time. Along with each measurement the system under observation had a distinct state at each point

2条回答
  •  情书的邮戳
    2020-11-28 05:13

    You could use np.diff() to test where a segment starts/ends and iterate over those results. Its a very simple solution, so probably not the most performent one.

    a = np.array([3,3,3,3,3,4,4,4,4,4,1,1,1,1,4,4,12,12,12])
    
    prev = 0
    splits = np.append(np.where(np.diff(a) != 0)[0],len(a)+1)+1
    
    for split in splits:
        print np.arange(1,a.size+1,1)[prev:split]
        prev = split
    

    Results in:

    [1 2 3 4 5]
    [ 6  7  8  9 10]
    [11 12 13 14]
    [15 16]
    [17 18 19]
    

提交回复
热议问题