Home > Enterprise >  How to iterate over window of next N elements for each row in pandas dataframe
How to iterate over window of next N elements for each row in pandas dataframe

Time:05-26

I have a problem with labeling rows in my dataframe. The idea is that I want to know whether current (iterated) value multiplied by small float is higher than any of next 60 rows in the dataframe.

I've already found a solution that works but unfortunately is very slow. I tried to make it with .apply and list comprehension but failed.

The code looks like this:

    for i in range(df.shape[0] - 1):
        label.append((df.iloc[i 1:window i]['value'] > df.iloc[i]['value'] * small_threshold).any())

So basically let's say that I have this df

idx value
0 2.00
1 2.01
2 1.98
3 2.01
4 2.02

I would like to label each row with respect to next 3 rows. To make it easier I will skip the multiplication of the current row value. So the goal is to label row as True if any value is higher than current value and False if there is no value (with respect to next 3 rows)

idx value label
0 2.00 true
1 2.01 true
2 1.98 false
3 2.01 NaN
4 2.02 NaN

CodePudding user response:

In your case check rolling

df['new'] = df.value.shift(-1).iloc[::-1].rolling(3).max().ge(df.value)
Out[56]: 
0     True
1     True
2    False
3    False
4    False
Name: value, dtype: bool
  • Related