Skip to content
Advertisement

How do I create a column in a pandas dataframe using values from two rows?

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

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement