0

How do I do this operation?

Initial Df :

    A B C D
0   0 1 0 0
1   1 1 1 1
2   0 0 1 1
3   0 1 0 1
4   1 1 0 0
5   1 1 1 0

Final Df :

    A B C D Param1 Param2 Param3 
0   0 1 0 0      0      0      0
1   1 1 1 1      3      1      0
2   0 0 1 1      0      2      2
3   0 1 0 1      1      1      1
4   1 1 0 0      1      1      1
5   1 1 1 0      1      2      0

Basically what I want to do is compare values on two consecutive rows and store the count of those comparison in a new column.

Details of the calculation :
For the first row (index 0): Param1, Param2, Param3 will be equal to 0

  1. Param1 : This is the count of the number of times a 0 from preview row changed to 1 in the current row, example(look at index 1) : three 0 from index 0 converts to 1, in index 1, hence Param1 for index 1 = 3
  2. Param2 : This is the count of the number of 1 from previous row which remain unchanged in the current row, example(look at index 3) : one 1 from index 2 remains unchanged, in index 3, hence Param2 for index 3 = 1
  3. Param3 : This is the count of the number of 1 from previous row which changed to 0 in the current row, example(look at index 2) : two 1 from index 1 changed to 0, in index 2, hence Param3 for index 3 = 2

2 Answers 2

4

You can just thake the diff and check for the above conditions as:

d = df.diff()
df.assign(param1=d.eq(1).sum(1),
          param2=(d.eq(0)&df.eq(1)).sum(1),
          param3=d.eq(-1).sum(1))

   A  B  C  D  param1  param2  param3
0  0  1  0  0       0       0       0
1  1  1  1  1       3       1       0
2  0  0  1  1       0       2       2
3  0  1  0  1       1       1       1
4  1  1  0  0       1       1       1
5  1  1  1  0       1       2       0
Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

shift = df.shift()
df['Param1'] = (df.eq(1) & shift.eq(0)).sum(1).astype(int)
df['Param2'] = (df.eq(1) & shift.eq(1)).sum(1).astype(int)
df['Param3'] = (df.eq(0) & shift.eq(1)).sum(1).astype(int)

Output:

   A  B  C  D  Param1  Param2  Param3
0  0  1  0  0       0       0       0
1  1  1  1  1       3       1       0
2  0  0  1  1       0       2       2
3  0  1  0  1       1       1       1
4  1  1  0  0       1       1       1
5  1  1  1  0       1       2       0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.