Skip to content
Advertisement

Find the row offset for the maximum value over the next N rows in Pandas?

I have some data in a Pandas DataFrame:

JavaScript

and I am trying to get the offset for the maximum of the next N rows. For example, when ****, the output would look like

JavaScript

I can get the value of the maximum over the next N rows using:

JavaScript

However, is it possible to get the row offset position for the maximum value of the next N rows using similar logic? Any pointers would be very helpful.

Advertisement

Answer

You can use rolling, but you need to cheat a bit by rolling on the reversed series.

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement