Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find index where elements change value pandas dataframe

Tags:

python

pandas

Regaring to this question/answer, is there a way to accomplish the same function for a pandas dataframe structure without casting it as a numpy array?

like image 366
ntmt Avatar asked Apr 20 '17 16:04

ntmt


1 Answers

Update: we can use this per @LorenzoMeneghetti

s[s.diff() != 0].index.tolist()

Output:

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

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]

like image 78
Scott Boston Avatar answered Sep 28 '22 07:09

Scott Boston