Reputation: 173
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?
Upvotes: 9
Views: 11679
Reputation: 153460
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]
Upvotes: 15