I have a simple question in pandas.
Lets say I have following data:
JavaScript
x
4
1
d = {'a': [1, 3, 5, 2, 10, 3, 5, 4, 2]}
2
df = pd.DataFrame(data=d)
3
df
4
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:
JavaScript
1
2
1
out = len(df.loc[df['a'].idxmin():df['a'].idxmax()])-2
2
output: 3
If the is a chance that the max is before the min:
JavaScript
1
2
1
out = max(len(df.loc[df['a'].idxmin():df['a'].idxmax()])-2, 0)
2
Alternative it the order of the min/max does not matter:
JavaScript
1
3
1
import numpy as np
2
out = np.ptp(df.reset_index()['a'].agg(['idxmin', 'idxmax']))-1
3
update: index of the 2nd largest/smallest:
JavaScript
1
8
1
# second largest
2
df['a'].nlargest(2).idxmin()
3
# 2
4
5
# second smallest
6
df['a'].nsmallest(2).idxmax()
7
# 3
8