Skip to content
Advertisement

Counting number of rows between min and max in pandas

I have a simple question in pandas.

Lets say I have following data:

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

How do I count the number of rows which are between minimum and maximum value in column a? So number of rows (it is 3 in this case) which are between 1 and 10 in this particular case?

Thanks

Advertisement

Answer

IIUC, you could get the index of the min and max, and subtract 2:

out = len(df.loc[df['a'].idxmin():df['a'].idxmax()])-2

output: 3

If the is a chance that the max is before the min:

out = max(len(df.loc[df['a'].idxmin():df['a'].idxmax()])-2, 0)

Alternative it the order of the min/max does not matter:

import numpy as np
out = np.ptp(df.reset_index()['a'].agg(['idxmin', 'idxmax']))-1

update: index of the 2nd largest/smallest:

# second largest
df['a'].nlargest(2).idxmin()
# 2

# second smallest
df['a'].nsmallest(2).idxmax()
# 3
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement