Skip to content
Advertisement

Pandas count number of rows since value > current cell

I have a Dataframe df:

JavaScript

I would like to count the number of rows backwards until a value greater than the current value is found, or until the first row is reached. For example, the result would be:

JavaScript

I am basically trying to find the value of n in df['col_1'].rolling(n).max() for each value of the series. How can I do this? Thank you in advance.

Advertisement

Answer

Numpy Approach

JavaScript

How it works?

  • Reverse the column col_1 and obtain a numpy view
JavaScript
  • Using broadcasting compare column a with itself
JavaScript
  • Using triu (upper triangular) mask the values to the left of main diagonal as we are only concerned with the values that come after the current value. Think of this as the main diagonal representing the current value.
JavaScript
  • Find the index of maximum value along the columns axis, this represents the index of first maximum after the current value. If there is no maximum value found we have update those indices to length of series
JavaScript
  • The above indices are zero based but we have to calculate the distance from the main diagonal therefore we can subtract these indices from the indices of main diagonal to calculate the distance (number of rows)
JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement