Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 739
Mahdi Avatar asked Nov 01 '25 11:11

Mahdi


1 Answers

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
like image 144
Shubham Sharma Avatar answered Nov 04 '25 01:11

Shubham Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!