Brian - 1 year ago 74

Python Question

I'm using a pandas series and I want to find the index value that represents the quantile.

If I have:

`np.random.seed(8)`

s = pd.Series(np.random.rand(6), ['a', 'b', 'c', 'd', 'e', 'f'])

s

a 0.873429

b 0.968541

c 0.869195

d 0.530856

e 0.232728

f 0.011399

dtype: float64

And do

`s.quantile(.5)`

I get

`0.70002511588475946`

What I want to know is what is the index value of

`s`

`d`

Answer Source

Use `sort_values`

, reverse the order, find all that are less than or equal to the quantile calculated, then find the `idxmax`

.

```
(s.sort_values()[::-1] <= s.quantile(.5)).idxmax()
```

Or:

```
(s.sort_values(ascending=False) <= s.quantile(.5)).idxmax()
```

We can functionalize it:

```
def idxquantile(s, q=0.5, *args, **kwargs):
qv = s.quantile(q, *args, **kwargs)
return (s.sort_values()[::-1] <= qv).idxmax()
idxquantile(s)
```