Skip to main content
v2026.11,858 entries · CC-BY 4.0

2 Sample Z Test (Two-Sample Z-Test): Formula, Example and Code

Two-sample z-test for means: formula, a fully worked example (z = 3.09), one- and two-tailed p-values, assumptions, APA report, and code.

Written and maintained by CASRAI Editorial Board

Last updated

The 2 sample z test (also written two-sample z-test or two sample z test) checks whether the means of two independent populations differ, using a standard normal test statistic. It applies when both population standard deviations are known, which is rare in practice; when they are not, the z-test vs t-test comparison explains the usual switch to a t-test. This page gives the formula, a worked example with every step of the arithmetic, one-tailed and two-tailed p-values, a confidence interval, an APA report and working code.

What is a 2 sample z test? (definition)

A two-sample z-test for means compares the average of one group with the average of another. The null hypothesis is that the two population means are equal (H0: μ1 − μ2 = 0). The test statistic is the observed difference between sample means divided by its standard error, and it follows a standard normal distribution when the assumptions below hold. A large absolute z means the observed difference would be unlikely if the population means were really equal. For a test of two proportions rather than means, see the two-proportion z-test.

Two-sample z-test formula and symbols

With known population standard deviations, the statistic is:

z = ((x̄1 − x̄2) − (μ1 − μ2)) / √(σ1²/n1 + σ2²/n2)

Under H0, μ1 − μ2 = 0, so the numerator reduces to x̄1 − x̄2.

  • x̄1, x̄2: the two sample means.
  • μ1 − μ2: the hypothesised difference between population means (0 under the usual null).
  • σ1, σ2: the known population standard deviations (not sample standard deviations).
  • n1, n2: the two sample sizes.
  • √(σ1²/n1 + σ2²/n2): the standard error of the difference between the means.

Worked example of a two sample z test (right-tailed and two-tailed)

Take two independent groups. Group 1: x̄1 = 15, σ1 = 2.3, n1 = 35. Group 2: x̄2 = 13.3, σ2 = 2.7, n2 = 48. The question is whether the Group 1 mean is greater than the Group 2 mean, at α = 0.05. This setup is adapted from the diet example in Berberyan, Nguyen and Swan (see Sources).

  1. Hypotheses. H0: μ1 − μ2 = 0. H1: μ1 − μ2 > 0 (right-tailed).
  2. Difference of means. 15 − 13.3 = 1.70.
  3. Variance terms. σ1²/n1 = 5.29/35 = 0.15114. σ2²/n2 = 7.29/48 = 0.15188.
  4. Standard error. √(0.15114 + 0.15188) = √0.30302 = 0.5505.
  5. Test statistic. z = 1.70 / 0.5505 = 3.09.
  6. Critical value. For a right-tailed test at α = 0.05, the critical z is 1.645 (see z-alpha). Since 3.09 > 1.645, reject H0.
  7. p-values. One-tailed p = P(Z ≥ 3.09) = 0.0010. Two-tailed p = 2 × 0.0010 = 0.0020.
  8. 95% confidence interval for μ1 − μ2. 1.70 ± 1.96 × 0.5505 = 1.70 ± 1.08, or 0.62 to 2.78. The interval excludes 0, which agrees with the rejection.

Interpreting the result

Result Reading
|z| larger than the critical value (1.645 one-tailed, 1.96 two-tailed, at α = 0.05) Reject H0: the data are inconsistent with equal population means at that α.
|z| smaller than the critical value Fail to reject H0. This is not evidence that the means are equal.
p ≤ α Same decision as the critical-value method.
Confidence interval excludes 0 Consistent with rejecting H0 in a two-tailed test at the matching confidence level.

The one-tailed p-value is exactly half the two-tailed p-value for the same z. Choose the tail before looking at the data; see two-tailed hypothesis testing. To convert a z-score to a p-value by hand, see how to find a p-value from a z-score.

Assumptions and common mistakes

Assumptions

  • Both samples are random samples.
  • The two samples are independent of each other, drawn from independent populations.
  • The population standard deviations are known.
  • Both sample sizes are at least 30, or both populations are normally distributed.

Two sample z test vs two-sample t-test

The most common mistake is using a z-test with sample standard deviations plugged in as though they were known population values. If σ is unknown, use a two-sample t-test (see t-test). One source notes that a good estimate s from a large sample (n above 30) can stand in for σ, but a known σ is the textbook condition.

Other mistakes

  • Paired data. If the two measurements come from the same units (before and after), the samples are not independent; test the mean of the differences instead.
  • Choosing the tail after seeing the data. Decide one-tailed or two-tailed in advance.
  • Treating a non-significant result as proof of no difference.

How to report a two-sample z-test (APA example)

For the example above, a two-tailed write-up in the style of an APA results sentence reads:

Group 1 (M = 15.00, n = 35) scored higher than Group 2 (M = 13.30, n = 48), z = 3.09, p = .002 (two-tailed), 95% CI of the difference [0.62, 2.78]. The population standard deviations were treated as known (σ1 = 2.3, σ2 = 2.7).

How to calculate a two sample z test in Excel, R or Python

Excel

The built-in Z.TEST function is a one-sample test, so compute the statistic directly. With the means in B1 and B2, the known standard deviations in C1 and C2 and the sample sizes in D1 and D2:

z:            =(B1-B2)/SQRT(C1^2/D1+C2^2/D2)
right-tailed: =1-NORM.S.DIST(z_cell,TRUE)
two-tailed:   =2*(1-NORM.S.DIST(ABS(z_cell),TRUE))

R

Base R has no z.test, so compute it directly with pnorm:

se <- sqrt(2.3^2/35 + 2.7^2/48)
z  <- (15 - 13.3) / se
pnorm(z, lower.tail = FALSE)       # right-tailed
2 * pnorm(abs(z), lower.tail = FALSE)  # two-tailed

If you have raw data, the DescTools package provides ZTest(x, y, alternative = "two.sided", mu = 0, sd_pop, conf.level = 0.95); the sd_pop argument (the known population standard deviation) has no default and must be supplied.

Python

from math import sqrt
from scipy.stats import norm

x1, s1, n1 = 15.0, 2.3, 35
x2, s2, n2 = 13.3, 2.7, 48

se = sqrt(s1**2 / n1 + s2**2 / n2)
z = (x1 - x2) / se
print(round(z, 2))                  # 3.09
print(round(norm.sf(z), 4))         # 0.001  right-tailed
print(round(2 * norm.sf(abs(z)), 4))  # 0.002  two-tailed

The ztest function in statsmodels (statsmodels.stats.weightstats.ztest) estimates the variances from the samples rather than taking known σ values, so it does not reproduce the known-sigma test above.

2 sample z test FAQ

When should I use a two-sample z-test instead of a t-test?

Use the z-test when both population standard deviations are known and the samples are independent, with both n at least 30 or both populations normal. When the standard deviations must be estimated from the samples, use a t-test.

What is the null hypothesis of a two sample z test?

That the difference between the two population means equals a stated value, almost always 0 (H0: μ1 − μ2 = 0).

What is the difference between a two-sample z-test and a two-proportion z-test?

The two-sample z-test compares means of a numerical outcome. The two-proportion z-test compares the proportions of a yes/no outcome.

Can I use a two sample z test for paired data?

No. Paired data are not independent samples; test the mean of the paired differences instead.

Sources

  • Berberyan, Nguyen and Swan. Statistics C1000: Introduction to Statistics, Citrus College, section 9.1, “z-Test for the Difference Between Two Means” (LibreTexts, CC BY 4.0). stats.libretexts.org. Checked 2026-10-29.
  • DescTools, ZTest reference documentation. rdrr.io. Checked 2026-10-29.
  • statsmodels, statsmodels.stats.weightstats.ztest. statsmodels.org. Checked 2026-10-29.
  • StatsRef, “Test for a mean when the standard deviation is known”. statsref.com. Checked 2026-10-29.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about 2 Sample Z Test (Two-Sample Z-Test): Formula, Example and Code

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.