Quartile Calculator

By · Last updated:

Quartile Calculator IQR = Q3 − Q1

Paste a list of numbers separated by commas, spaces or new lines. You get Q1, the median, Q3, the interquartile range, the 1.5 × IQR outlier fences, any outliers, and a box plot — with the working for two quartile methods.

Examples:

Quartiles are the three values Q1, Q2 and Q3 that cut a sorted data set into four parts holding a quarter of the observations each; Q2 is the median. This quartile calculator returns the five-number summary, the interquartile range (IQR), the 1.5 × IQR outlier fences, the outliers themselves and a box plot, using either the textbook method or NumPy’s.

What are quartiles and the five-number summary?

Sort the data from smallest to largest. The first quartile Q1 is the value a quarter of the way up, the second quartile Q2 is the median, and the third quartile Q3 sits three quarters of the way up. Half of the observations fall between Q1 and Q3, and the width of that middle half is the interquartile range, IQR = Q3 − Q1.

The five-number summary is the minimum, Q1, the median, Q3 and the maximum. Those five numbers describe the centre, the spread and the skew of a variable without assuming any distribution, and they are exactly what a box plot draws. Quartiles are the 25th, 50th and 75th percentiles, so the percentile calculator handles any other cut point you need.

How do you calculate quartiles?

There is no single agreed formula, which is why this tool offers two. Both start from the sorted values and agree on the median; they differ in how Q1 and Q3 are located.

Exclusive method. Split the sorted data at the median. When the count \(n\) is odd, the median itself is left out of both halves. Q1 is the median of the lower half and Q3 is the median of the upper half:

\[ Q_1 = \operatorname{median}\big(x_{(1)},\dots,x_{(\lfloor n/2 \rfloor)}\big), \qquad Q_3 = \operatorname{median}\big(x_{(n-\lfloor n/2 \rfloor+1)},\dots,x_{(n)}\big) \]

Linear interpolation. For a fraction \(q\) (0.25, 0.5 or 0.75), compute a position on the sorted data, counting from index 0, and interpolate between the two neighbouring values:

\[ h = (n-1)\,q, \qquad Q = x_{[\lfloor h \rfloor]} + \big(h - \lfloor h \rfloor\big)\big(x_{[\lfloor h \rfloor + 1]} - x_{[\lfloor h \rfloor]}\big) \]

Here \(n\) is the number of values, \(x_{(k)}\) is the \(k\)-th smallest value counting from 1, \(x_{[i]}\) is the same sorted list indexed from 0, and \(\lfloor h \rfloor\) is \(h\) rounded down. Whichever method you choose, the spread and the outlier fences follow from Q1 and Q3:

\[ \mathrm{IQR} = Q_3 - Q_1, \qquad \text{lower fence} = Q_1 - 1.5\,\mathrm{IQR}, \qquad \text{upper fence} = Q_3 + 1.5\,\mathrm{IQR} \]

Worked example: eleven values

The default input is the sorted list 6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49, so \(n = 11\). The median is the sixth value, 40. With the exclusive method the lower half is 6, 7, 15, 36, 39 and the upper half is 41, 42, 43, 47, 49, each with five values, so Q1 is the middle of the lower half, 15, and Q3 is the middle of the upper half, 43.

\[ \mathrm{IQR} = 43 - 15 = 28, \qquad 15 - 1.5 \times 28 = -27, \qquad 43 + 1.5 \times 28 = 85 \]

Every value lies between −27 and 85, so nothing is flagged and the whiskers of the box plot reach the minimum 6 and the maximum 49. Notice how lopsided the box is: Q1 is 25 units below the median while Q3 is only 3 above it, which is the signature of a left-skewed sample.

Why do textbook quartiles and NumPy quartiles differ?

Switch the method to linear interpolation and the same eleven numbers give different quartiles. For Q1 the position is \(h = 10 \times 0.25 = 2.5\), halfway between index 2 (value 15) and index 3 (value 36), so Q1 = 15 + 0.5 × 21 = 25.5. For Q3 the position is \(h = 7.5\), halfway between 42 and 43, so Q3 = 42.5.

QuantityExclusive (textbook)Linear interpolation (NumPy, pandas)
Minimum66
Q11525.5
Median (Q2)4040
Q34342.5
Maximum4949
IQR2817
Lower fence−270
Upper fence8568
Outliersnonenone

Neither column is wrong. A quartile of a small sample is an estimate, and the two conventions place the 25% and 75% marks at slightly different points between the same data values. The gap is large here because 15 and 36 are far apart; on a few thousand rows the two methods agree to several decimal places. What matters is that you report which rule you used and do not mix them. NumPy’s percentile documentation lists linear interpolation as the default and describes the alternative estimators available through its method argument.

What is the 1.5 × IQR outlier rule?

A value is flagged as an outlier when it lies more than one and a half interquartile ranges outside the box: \(x \lt Q_1 - 1.5\,\mathrm{IQR}\) or \(x \gt Q_3 + 1.5\,\mathrm{IQR}\). Take 1, 2, 3, 4, 5, 6, 7, 8, 100 (the With outlier preset). The exclusive method gives Q1 = 2.5, a median of 5 and Q3 = 7.5, so IQR = 5 and the fences are −5 and 15. The value 100 is far beyond 15 and is drawn as a separate dot, while the upper whisker stops at 8.

Because quartiles depend on rank rather than magnitude, replacing 100 with 1,000,000 leaves Q1, Q3 and the fences untouched. The mean, by contrast, would jump from about 15.1 to more than 111,000. That resistance is the reason the rule works: the yardstick used to judge extreme values is not itself distorted by them.

Where are quartiles used in machine learning?

Robust scaling. Standardisation subtracts the mean and divides by the standard deviation, and both are dragged around by extreme values. Robust scaling subtracts the median and divides by the IQR instead. scikit-learn’s RobustScaler does this per feature with a default quantile range of 25 to 75. On the worked example with interpolated quartiles, the value 49 becomes (49 − 40) / 17 = 0.529 and the value 6 becomes (6 − 40) / 17 = −2.

Outlier filtering before training. Least-squares models are sensitive to extreme targets and features, because squared error lets one distant point outweigh many ordinary ones; you can see the effect by adding a single extreme point in the linear regression calculator. A common preprocessing step is to drop or clip rows outside the fences, computed on the training split only so that no information leaks from the test set.

Exploratory data analysis. A box plot per feature, or per class, shows skew, spread and suspicious values in one glance, which helps you decide on a log transform or on a median rather than a mean as the summary. The mean, as the expected value of a variable, is the right summary for symmetric data; the median and IQR are safer when the distribution has a long tail.

Common mistakes

  • Forgetting to sort. Quartiles are defined on ordered data. Splitting the list in the order it was collected gives meaningless halves.
  • Mixing methods. Computing Q1 by hand with the exclusive rule and Q3 from np.percentile produces an IQR that belongs to neither convention.
  • Keeping the median in the halves when the count is odd. Including it is a third convention and changes the answer: on the eleven values above it gives 25.5 and 42.5 rather than 15 and 43.
  • Treating every flagged point as an error. The fences mark values worth inspecting. Heavy-tailed variables such as income or request latency produce many legitimate points beyond them.
  • Computing fences on the full data set. Fit the quartiles on training data and apply the same fences to validation and test data.

Quartiles in Python with NumPy

The code below reproduces both columns of the table for the worked example.

import numpy as np

x = np.array([6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49])

# Linear interpolation (NumPy default)
q1, q2, q3 = np.percentile(x, [25, 50, 75])
print(q1, q2, q3)            # 25.5 40.0 42.5

# Exclusive method: medians of the two halves
s = np.sort(x)
h = len(s) // 2
q1 = np.median(s[:h])
q3 = np.median(s[len(s) - h:])
print(q1, q3)                # 15.0 43.0

iqr = q3 - q1                # 28.0
lo = q1 - 1.5 * iqr          # -27.0
hi = q3 + 1.5 * iqr          # 85.0
print(s[(s < lo) | (s > hi)])  # [] (no outliers)

pandas.Series.quantile also interpolates linearly by default, so pd.Series(x).quantile([0.25, 0.75]) returns 25.5 and 42.5 as well.

Frequently asked questions

How do I find Q1 and Q3 by hand?

Sort the values and find the median. Split the data into a lower and an upper half, leaving the median out if the count is odd. Q1 is the median of the lower half and Q3 is the median of the upper half. For 1, 2, 3, 4, 5, 6, 7, 8 the halves are 1 to 4 and 5 to 8, so Q1 = 2.5 and Q3 = 6.5.

Why does NumPy give a different quartile than my textbook?

NumPy places each quartile at position (n − 1) × q on the sorted data and interpolates between neighbours, while most textbooks take the medians of the two halves. For 1 to 8 NumPy returns Q1 = 2.75 and Q3 = 6.25 against the textbook 2.5 and 6.5. Both are valid estimates and they converge as the sample grows.

Is Q2 the same as the median?

Yes. The second quartile is the 50th percentile, which is the median, and both methods in this calculator return the same value for it. Only Q1 and Q3 depend on the convention.

Can the IQR be zero?

Yes. If at least half of the values are identical, Q1 and Q3 can coincide and the IQR is 0. Both fences then collapse onto that value and every different observation is flagged, so the 1.5 × IQR rule is not useful for such data.

Related calculators

Browse every tool for describing and testing data in the statistics calculators hub, or find any cut point between 0 and 100 with the percentile rank and value tool.

More tools that pair with this one: