Home > Blockchain >  Counting number of rows between min and max in pandas
Counting number of rows between min and max in pandas

Time:03-24

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

CodePudding user response:

You can use this:

diff = np.abs(df['a'].idxmin() - df['a'].idxmax()) - 1

CodePudding user response:

Since they are numbers, we can dump down into numpy and compute the result:

arr = df.a.to_numpy()
np.abs(np.argmax(arr) - np.argmin(arr)) - 1
3

CodePudding user response:

np.abs(df.loc[df.a==df.a.min()].index-df.loc[df.a==df.a.max()].index)-1

CodePudding user response:

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
  • Related