Skip to content
Advertisement

Create forth column based on other columns (lagged) values

JavaScript

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

I would like to create a forth column “D” which will take a value of 1 if:

  • (at least) two column (A, B, C) have a value of 1 or
  • the previous 2 periods had at least two columns with a value of 1.

According to the example above all the rows would have df['D']==1

Advertisement

Answer

We can look for the 3-window rolling sum of a boolean series that marks at least 2 ones per row, and check if the result is 0 (so that D will be too) or not:

JavaScript

samples:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement