Slicing pandas dataframe on equal column values

pr94 :

I have a pandas df that looks like this:

import pandas as pd

df = pd.DataFrame({0:[1],5:[1],10:[1],15:[1],20:[0],25:[0],
                   30:[1],35:[1],40:[0],45:[0],50:[0]})
df

enter image description here

The column names reflect coordinates. I would like to retrieve the start and end coordinate of columns with consecutive equal numbers.

The output should be something like this:

# start,end
0,15 
20,25
30,35
40,50
YOBEN_S :

IIUCusing groupby with diff and cumsum to split the group

s=df.T.reset_index()
s=s.groupby(s[0].diff().ne(0).cumsum())['index'].agg(['first','last'])
Out[241]: 
   first  last
0             
1      0    15
2     20    25
3     30    35
4     40    50

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=29001&siteId=1