Python Pandas - how is 25 percentile calculated by describe function
<p>For a given dataset in a data frame, when I apply the <code>describe</code> function, I get the basic stats which include min, max, 25%, 50% etc.</p>

<p>For example:</p>

<pre><code>data_1 = pd.DataFrame({'One':[4,6,8,10]},columns=['One'])
data_1.describe()
</code></pre>

<p>The output is:</p>

<pre><code>        One
count   4.000000
mean    7.000000
std     2.581989
min     4.000000
25%     5.500000
50%     7.000000
75%     8.500000
max     10.000000
</code></pre>

<p><strong>My question is</strong>: What is the mathematical formula to calculate the 25%?</p>

<p>1) Based on what I know, it is:</p>

<pre><code>formula = percentile * n (n is number of values)
</code></pre>

<p>In this case:</p>

<pre><code>25/100 * 4 = 1
</code></pre>

<p>So the first position is number 4 but according to the describe function it is <code>5.5</code>.</p>

<p>2) Another example says - if you get a whole number then take the average of 4 and 6 - which would be 5 - still does not match <code>5.5</code> given by describe.</p>

<p>3) Another tutorial says - you take the difference between the 2 numbers - multiply by 25% and add to the lower number:</p>

<pre><code>25/100 * (6-4) = 1/4*2 = 0.5
</code></pre>

<p>Adding that to the lower number: <code>4 + 0.5 = 4.5</code></p>

<p>Still not getting <code>5.5</code>.</p>

<p>Can someone please clarify?</p>

<p>In the <a href="https://github.com/pydata/pandas/blob/master/pandas/core/series.py">pandas documentation</a> there is information about hte computation of quantiles, where a reference to numpy.percentile is made: </p>

<blockquote>
  <p>Return value at the given quantile, a la numpy.percentile.</p>
</blockquote>

<p>Then, checking numpy.percentile <a href="http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.percentile.html">explanation</a>, we can see that the interpolation method is set to <strong>linear</strong> by default:</p>

<blockquote>
  <p>linear: i + (j - i) * fraction, where fraction is the fractional part
  of the index surrounded by i and j</p>
</blockquote>

<p>For your specfic case, the 25% quantile results from:</p>

<p>res_25 = 4 + (6-4)*(3/4) =  5.5</p>

<p>For the 75% quantile we thus get: </p>

<p>res_75 = 8 + (10-8)*(1/4) = 8.5</p>

<p>If you set the interpolation method to "midpoint", then you will the results, that you thought of. </p>

<p>.</p>

python pandas percentile