Juliana Rivera Juliana Rivera - 1 month ago 7
Python Question

Pandas+Python - How to know when a value changes?

I've been working on a DataFrame, like the following extract and I want to know when the value changes:

A M C
0 2.0 1 C1
1 2.0 1 C1
2 2.0 2 C1
3 2.0 2 C1
4 2.0 3 C1
5 2.0 3 C1
6 2.0 1 C2
7 2.0 1 C2
8 2.0 2 C2
9 2.0 2 C3
10 2.0 3 C3
11 2.0 3 C3
13 2.1 1 C3
14 2.1 1 C3
15 2.1 2 C3
16 2.1 2 C3
17 2.1 3 C3
18 2.1 3 C3


I know that A or C, changes always when M starts in 1. The question is how can I get the position every time M value starts in 1?

Answer

I don't know if your entire data set is built the same way as the one you are showing us but from what I can see you are searching for occurrence of 3 to 1 in the m columns which would result in a difference of -2 :

df[df['M'].diff()==-2].index
Out[101]: Int64Index([6, 13], dtype='int64')

let's say your M column always increases but it can go higher than 3, you could just look for the first occurrence of a negative number such has:

df[df['M'].diff()<0].index
Out[103]: Int64Index([6, 13], dtype='int64')

let's say there is no pattern there you could simply do:

df[(df['M'].diff()!=0) & (df['M']==1)].index
Out[104]: Int64Index([0, 6, 13], dtype='int64')

this is adding index 0 because .diff() will return NaN for the first index of the dataframe which is !=0 and df['M'] ==0