Home > Blockchain >  Count the rows between first and second largest values in pandas
Count the rows between first and second largest values in pandas

Time:03-25

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

CodePudding user response:

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

CodePudding user response:

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

  • Related