'Count the rows between first and second largest values in pandas

This is question is similar to the one I have posted here. But with a little different framework.

Let's say I have the pandas data frame:

d = {'a': [12, 3, 5, 2, 10, 3, 5, 4, 2]}
df = pd.DataFrame(data=d)
df

I want to count a total number of rows, between the first and the second max values of column a. In this case, it will be 3, which is the number of rows between 12 and 10. How can one do it more generically?

Thanks



Solution 1:[1]

Use Series.nlargest:

In [2451]: l = df['a'].nlargest(2).index # Find indexes of top 2 largest values
In [2455]: res = abs(l[0] - l[1]) - 1 # Get the absolute diff and subtract 1

In [2456]: res
Out[2456]: 3

Solution 2:[2]

Using numpy.ptp:

out = np.ptp(df['a'].nlargest(2).index.to_numpy())-1

or, to work with any initial index (not only range):

out = np.ptp(df.reset_index()['a'].nlargest(2).index.to_numpy())-1

output: 3

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 mozway