1

I have data like this-

    User1  User2  User3  User4  User5  User6  User7  User8
w1      1      1      1      1      0      1      1      1
w2      0      1      0      0      1      1      1      1
w3      0      0      1      1      1      1      1      1
w4      1      1      1      0      0      0      0      1
w5      1      0      1      0      1      1      1      0
w6      1      1      1      1      1      1      1      1

Now what I want to do is compare every two consecutive weeks, and find all cases where the change is 1->0 .

So output for the above data would be something like this-

    Column
w1      n/a    
w2      3   
w3      1
w4      4   
w5      2 
w6      0

2 Answers2

2

could also do it like this:

(df > df.shift(-1)).sum(axis=1)
Derek Eden
  • 3,457
  • 1
  • 8
  • 25
  • 1
    This works, but the final df was one row shifted up as compared to the one that I wanted. Used shift to get it the way I wanted, thanks! – Vivek Singh Jul 18 '20 at 19:43
0

Let say the dataframe name is df, so you can achieve it by

abs(df.diff()).sum(axis=1)
Reza
  • 1,570
  • 1
  • 4
  • 16