I have a DataFrame from which I'm filtering the values. Lets say that my data is x=[1 1 1 2 2 3 3 3 1 1 1 3 3 1 1];
and I'd like to have all the indexes where 3 is located. So I'm executing data=df[df.x==3]
to get the list data.index=[5,6,7,11,12]
Now I'd like to generate a narrow list which will provides me the ranges where 3 is located:
[[5, 7] [11, 12]]
. Is there a simple way to do it rather than write a function for this?
In Matlab I'm doing this by following (idx
- the list of indexes where 3 is located and a
is an output)
a(1,1)=idx(1);
j=1;
for i=2:length(idx)
if (idx(i) ~= (idx(i-1)+1))
a(j,2)=idx(i-1);
if i < length(idx)
j=j+1;
a(j,1)=idx(i);
end
end
end
a(j,2)=idx(end);