user3318023 user3318023 - 24 days ago 10
Python Question

Calculate changes of column in Pandas

I have a dataframe with data and I want calculate changes of values during time.

UserId DateTime Value
1 1 0
1 2 0
1 3 0
1 4 1
1 6 1
1 7 1
2 1 0
2 2 1
2 3 1
2 4 0
2 6 1
2 7 1


So after script execution I want to get a column with change identifier (for user and date). Only changes from 0 to 1 is interesting.

UserId DateTime Value IsChanged
1 1 0 0
1 2 0 0
1 3 0 0
1 4 1 1 <- Value was changed from 0 to 1
1 6 1 0
1 7 1 0
2 1 0 0
2 2 1 1 <- Value was changed from 0 to 1
2 3 1 0
2 4 0 0 <- Change from 1 to 0 not interesting
2 6 1 1 <- Value was changed from 0 to 1 for the user
2 7 1 0

Answer

What about this?

# df is your dataframe    
df['IsChanged'] =  (df['Value'].diff()==1).astype(int)   

The only case you care about is Value being 0 before and 1 after, so you can simply calculate the change in value and check if it is equal to 1.

    UserId  DateTime  Value  IsChanged
0        1         1      0          0
1        1         2      0          0
2        1         3      0          0
3        1         4      1          1
4        1         6      1          0
5        1         7      1          0
6        2         1      0          0
7        2         2      1          1
8        2         3      1          0
9        2         4      0          0
10       2         6      1          1
11       2         7      1          0