How do I calculate percentiles with python/numpy?
Question: Is there a convenient way to calculate percentiles for a sequence or single-
dimensional numpy array?

I am looking for something similar to Excel's percentile function.

I looked in NumPy's statistics reference, and couldn't find this. All I could
find is the median (50th percentile), but not something more specific.


Answer: You might be interested in the [SciPy
Stats](http://docs.scipy.org/doc/scipy/reference/stats.html) package. It has
[the percentile
function](http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.scoreatpercentile.html#scipy.stats.scoreatpercentile)
you're after and many other statistical goodies.

`percentile()` [is
available](http://docs.scipy.org/doc/numpy/reference/generated/numpy.percentile.html)
in `numpy` too.

    
    
    import numpy as np
    a = np.array([1,2,3,4,5])
    p = np.percentile(a, 50) # return 50th percentile, e.g median.
    print p
    3.0
    

~~[This ticket](http://projects.scipy.org/numpy/ticket/626) leads me to
believe they won't be integrating `percentile()` into numpy anytime soon.~~

