How to Perform Welch’s t-test in Python

The most common way to compare the means between two independent groups is to use a two-sample t-test. However, this test assumes that the variances between the two groups is equal.

If you suspect that the variance between the two groups is not equal, then you can instead use Welch’s t-test, which is the non-parametric equivalent of the two-sample t-test.

To perform Welch’s t-test in Python, we can use the ttest_ind() function from the SciPy library, which uses the following syntax:

ttest_ind(a, b, equal_var=False)


  • a: First array of data values
  • b: Second array of data values
  • equal_var: Specifies no assumption of equal variances between the two arrays

This tutorial explains how to use this function to perform Welch’s t-test in Python.

Example: Welch’s t-test in Python

Suppose we want to compare the exam scores of 12 students who used an exam prep booklet to prepare for some exam vs. 12 students who did not.

The following code shows how to perform Welch’s t-test in Python to determine if the mean exam scores are equal between the two groups:

#import ttest_ind() function
from scipy import stats

#define two arrays of data
booklet = [90, 85, 88, 89, 94, 91, 79, 83, 87, 88, 91, 90]
no_booklet = [67, 90, 71, 95, 88, 83, 72, 66, 75, 86, 93, 84]

#perform Welch's t-test 
stats.ttest_ind(booklet, no_booklet, equal_var = False)

Ttest_indResult(statistic=2.23606797749, pvalue=0.04170979503207)

The test statistic turns out to be 2.2361 and the corresponding p-value is 0.0417.

Since this p-value is less than .05, we can reject the null hypothesis of the test and conclude that there is a statistically significant difference in mean exam scores between the two groups.

Note that the two sample sizes in this example were equal, but Welch’s t-test still works even if the two sample sizes are not equal.

Additional Resources

An Introduction to Welch’s t-test
Welch’s t-test Calculator
How to Perform Welch’s t-test in Excel

Leave a Reply

Your email address will not be published. Required fields are marked *