Variance in Excel, Google Sheets, Python and R
Every tool can calculate variance, but they do not all mean the same thing by it. This guide lists the right function in each, and the defaults that quietly change your answer.
By the VarianceCalc team · Updated
Open the variance calculatorQuick reference
All results below use the data set 2, 4, 4, 4, 5, 5, 7, 9, whose sample variance is ≈ 4.5714 and population variance is 4.
| Tool | Sample variance (÷ n − 1) | Population variance (÷ n) |
|---|---|---|
| Excel | =VAR.S(A1:A8) | =VAR.P(A1:A8) |
| Google Sheets | =VAR.S(A1:A8) or =VAR(A1:A8) | =VAR.P(A1:A8) or =VARP(A1:A8) |
| Python (standard library) | statistics.variance(data) | statistics.pvariance(data) |
| numpy | np.var(data, ddof=1) | np.var(data) |
| pandas | df["col"].var() | df["col"].var(ddof=0) |
| R | var(x) | var(x) * (length(x) - 1) / length(x) |
Excel
Type your values into a column, say A1 to A8, then in an empty cell enter=VAR.S(A1:A8) for sample variance or =VAR.P(A1:A8) for population variance. Excel returns 4.571428571 and 4 respectively. For standard deviation, the matching functions are STDEV.S and STDEV.P.
Excel has six variance functions, which is where confusion starts:
| Function | Divides by | Text and TRUE/FALSE in a range |
|---|---|---|
VAR.S | n − 1 | Ignored |
VAR.P | n | Ignored |
VAR | n − 1 | Ignored (legacy name for VAR.S) |
VARP | n | Ignored (legacy name for VAR.P) |
VARA | n − 1 | Text counts as 0, TRUE as 1 |
VARPA | n | Text counts as 0, TRUE as 1 |
The A versions are rarely what you want. A cell containing "n/a" becomes a zero, which drags the mean down and inflates the variance without any warning.
Common Excel problems
- Numbers stored as text. Values pasted from a web page or PDF are often left-aligned text.
VAR.Sskips them silently, so the result uses fewer values than you think. Check with=COUNT(A1:A8). #DIV/0!error.VAR.Sneeds at least two numbers. With one, the denominatorn − 1is zero.- Hidden or filtered rows.
VAR.Sincludes hidden rows. Use=SUBTOTAL(110, A1:A100)(sample) or=SUBTOTAL(111, A1:A100)(population) to work on visible rows only.
Google Sheets
Google Sheets accepts the same names as Excel: VAR.S, VAR.P,VAR, VARP, VARA and VARPA, with the same meanings. Formulas copy between the two without changes.
Python
Standard library
import statistics
data = [2, 4, 4, 4, 5, 5, 7, 9]
statistics.variance(data) # 4.571428571428571 (sample)
statistics.pvariance(data) # 4 (population)numpy
import numpy as np
np.var(data) # 4.0 (population, the default)
np.var(data, ddof=1) # 4.571428571428571 (sample)pandas
import pandas as pd
s = pd.Series(data)
s.var() # 4.571428571428571 (sample, the default)
s.var(ddof=0) # 4.0 (population)Watch the defaults. numpy uses ddof=0 (population) and pandas uses ddof=1 (sample). Moving a calculation from one library to the other changes the answer unless you set ddof explicitly. The name stands for "delta degrees of freedom": the denominator is n − ddof.
R
x <- c(2, 4, 4, 4, 5, 5, 7, 9)
var(x) # 4.571429 (sample)
var(x) * (length(x) - 1) / length(x) # 4 (population)R's built-in var() and sd() always use n − 1. There is no population option, so rescale by (n − 1) / n as shown. Missing values make the result NA unless you add na.rm = TRUE.
Checking your answer
If two tools disagree, the cause is nearly always one of three things: one divides byn and the other by n − 1; some values were skipped as text or blanks; or a missing value was treated as zero. Paste the same numbers into thevariance calculator to get both versions side by side with the working shown. It also tells you which entries it could not read as numbers instead of dropping them quietly.
Frequently asked questions
Should I use VAR.S or VAR.P in Excel?
Use VAR.S when your data is a sample of a larger group, which is the usual case. Use VAR.P only when the range contains the entire population you care about.
What is the difference between VAR and VAR.S?
None in the result. VAR is the older name kept for compatibility and also calculates sample variance. Microsoft recommends VAR.S in new workbooks.
Why does numpy give a different variance from Excel?
numpy.var divides by n by default (population variance), while Excel's VAR.S divides by n − 1. Pass ddof=1 to numpy.var to match VAR.S.
Does Excel's VAR.S include blank cells or text?
No. When you pass a cell range, VAR.S and VAR.P ignore empty cells, text and logical values. VARA and VARPA count text as 0 and TRUE as 1.
Keep learning
- What Is Variance?What variance measures, how to read it, and where it shows up in grades, investing and quality control.
- How to Calculate VarianceThe five-step method by hand, with a fully worked example and the mistakes to avoid.
- The Variance Formula ExplainedEvery symbol in σ² and s², the shortcut formula, and the formula for grouped (frequency) data.
- Sample vs Population VarianceWhen to divide by n − 1, a small proof that it works, and a checklist for picking the right one.
- Standard Deviation CalculatorSample and population standard deviation, how it relates to variance, and how to interpret it.
- Percent Variance CalculatorVariance between two numbers, such as budget vs actual, as an amount and a percentage.