0Pricing
Pandas & NumPy Academy · Lesson

T-Tests for Comparing Means

Run one-sample, independent two-sample, and paired t-tests with scipy.stats and interpret confidence intervals.

What Is a T-Test?

A t-test is a hypothesis test that determines whether a difference in means between groups is statistically significant or likely due to random chance. It compares the observed difference to the variability in the data, producing a t-statistic and a p-value. If p ≤ 0.05 (the conventional threshold), we reject the null hypothesis that the means are equal. There are three common types: one-sample, independent two-sample, and paired t-test, each for different experimental designs.

One-Sample T-Test

The one-sample t-test tests whether the mean of a sample differs significantly from a known or hypothesised population mean. For example: 'Is our average delivery time significantly different from the industry standard of 3 days?' Use scipy.stats.ttest_1samp(data, popmean). The null hypothesis is H₀: mean = popmean. A small p-value leads you to reject H₀ and conclude the sample mean differs from the target.

import numpy as np
from scipy import stats

np.random.seed(42)
# Delivery times in days (true mean is ~3.5, not 3)
delivery_times = np.random.normal(loc=3.5, scale=0.8, size=50)

# Test: is our mean significantly different from 3 days?
stat, p = stats.ttest_1samp(delivery_times, popmean=3.0)
print(f'Sample mean: {delivery_times.mean():.3f}')
print(f't-statistic: {stat:.3f}')
print(f'p-value: {p:.4f}')
print('Differs from 3?', 'Yes' if p < 0.05 else 'No')

All lessons in this course

  1. Descriptive Stats and Normality Testing
  2. T-Tests for Comparing Means
  3. Chi-Squared Test for Independence
  4. ANOVA and Post-Hoc Tests
← Back to Pandas & NumPy Academy