Home > Net >  How to iterate throughout the column by comparing two row values in one iteration in python?
How to iterate throughout the column by comparing two row values in one iteration in python?

Time:11-29

Here in this excel analysis, there is a condition that was used, Excel formula=(=IF(AND(B2<1;B3>5);1;0)),please refer the image below. (https://i.stack.imgur.com/FpPIK.png)

if compressor-1 first-row value is less than 1 (<1) and the second-row value is greater than 5 (>5) then it will return value '1', if the condition is not satisfied it will return value'0'. Even if one row satisfied the condition and the other row doesn't it will return '0'

( for first output 1st &2nd rows,for second output 2nd &3rd rows and so on for the rest of the rows)

So, I have tried in jupyter notebook to write a code that iterates through all rows in 1 column by comparing with this condition

df3['cycle']=0&1
df3.loc[(df3['Kompressor_1_val']<1&5),['cycle']]=0
df3.loc[(df3['Kompressor_1_val']>1&5),['cycle']]=1
df3

But could anyone please help me to write the code by considering the above Excel analysis? In the new column, I need to get the output by satisfying this condition-output should be 0 or 1 based on the following description which was provided in excel analysis

i.e for 1st iteration, it should compare 1st row and 2nd row of the selected column with the condition to give the output either 1 or 0

for 2nd iteration, it should compare the 2nd row and 3rd row of the selected column with the condition to give the output either 1 or 0 and so on for the rest of the rows.

(https://i.stack.imgur.com/dCuMr.png)

CodePudding user response:

You can check the current Compressor 1 row using .lt(...)

df["Compressor 1"].lt(1)

And the next row using .shift(-1) and .gt(...)

df["Compressor 1"].shift(-1).gt(5)

Put them together with & and convert to int

df["Frequency Cycle Comp 1"] = (df["Compressor 1"].lt(1) & df["Compressor 1"].shift(-1).gt(5)).astype(int)

An example

import pandas as pd
import numpy as np


np.random.seed(0)

df = pd.DataFrame(np.random.randint(low=-10, high=10, size=(10,)), columns=["Compressor 1"])

df["Frequency Cycle Comp 1"] = (df["Compressor 1"].lt(1) & df["Compressor 1"].shift(-1).gt(5)).astype(int)

print(df)
   Compressor 1  Frequency Cycle Comp 1
0             2                       0
1             5                       0
2           -10                       0
3            -7                       0
4            -7                       0
5            -3                       0
6            -1                       1
7             9                       0
8             8                       0
9            -6                       0
  • Related