Find index where elements change value pandas dataframe

Going back to this question / answer , is there a way to accomplish the same function for a pandas data structure without allocating it as a numpy array?

+3


source to share


1 answer


s = pd.Series([1, 1, 1, 1, 1, 2, 2, 2, 3, 4, 3, 4, 3, 4, 3, 4, 5, 5, 5])

print(s.diff()[s.diff() != 0].index.values)

      

OR

df = pd.DataFrame([1, 1, 1, 1, 1, 2, 2, 2, 3, 4, 3, 4, 3, 4, 3, 4, 5, 5, 5])

print(df[0].diff()[df[0].diff() != 0].index.values)

      



Output:

[0 5 8 9 10 11 12 13 14 15 16]

+1


source







All Articles