Home > Enterprise >  Check if values in all n previous rows are greater than the value of current row
Check if values in all n previous rows are greater than the value of current row

Time:07-20

I have a pandas dataframe like this:

    col_name
0      2
1      3
2      1
3      0
4      5
5      4
6      3
7      3

that could be created with the code:

import pandas as pd

dataframe = pd.DataFrame(
    {
        'col_name': [2, 3, 1, 0, 5, 4, 3, 3]
    }
)

Now, I want to get the rows which have a value less than the values in all the n previous rows. So, for n=2 the output is the rows: 2, 3, 6.

Additionally I don't want to use any for-loops in my code.

Just a trick that comes to my mind is that we can count the number of rows in n previous rows having a value less than the current row and then check if the number equals to n.

Have you any idea for the code with my trick or in any other ways? and also is there a way to write a similar code except that if we want to check if values in n next rows are less than the value of the current row?

CodePudding user response:

Let us use rolling to calculate the min value in n previous rows then compare the min value with current row to create a boolean mask

df[df['col_name'] < df['col_name'].shift().rolling(2, min_periods=1).min()]

   col_name
2         1
3         0
6         3
  • Related