📚 Trusted by students, educators & researchers since 2017.
When you want to compare the averages of two independent groups (like test scores from Class A vs. Class B), the standard go-to tool is the independent t-test. However, the t-test has a strict rule: your data must be roughly normally distributed (shaped like a bell curve).
What happens if your data is heavily skewed, has massive outliers, or is based on an ordinal ranking scale (like a 5-star review system)? You use the Mann-Whitney U Test.
What is the Mann-Whitney U Test?
The Mann-Whitney U test (also called the Wilcoxon rank-sum test) is a non-parametric statistical test. Instead of comparing the means (averages) of two groups, it compares their ranks.
It evaluates a very specific question: If you pull one random value from Group A and one random value from Group B, what is the probability that Group A’s value is larger? If the two groups are identical, that probability should be 50/50.
How it Works (The Math)
Instead of calculating averages, the Mann-Whitney test completely transforms your data into ranks.
- Combine and Rank: All data points from both groups are combined into one master list and sorted from smallest to largest. The smallest number gets a rank of 1, the next gets 2, and so on. (If there is a tie, they share the average of their ranks).
- Sum the Ranks: The groups are split back apart, and the ranks for each group are added together to get $R_1$ and $R_2$.
- Calculate U: The $U$-statistic is calculated for both groups using their sample sizes ($n_1$ and $n_2$) and their rank sums.
$$U_1 = n_1 n_2 + \frac{n_1(n_1+1)}{2} – R_1$$
$$U_2 = n_1 n_2 + \frac{n_2(n_2+1)}{2} – R_2$$
The final $U$-statistic reported is simply the smaller of those two numbers: $U = \min(U_1, U_2)$. For large sample sizes, this $U$ value is converted into a standard Z-score to determine the p-value.
How to Run a Mann-Whitney U Test in Python
You can easily run this test using the scipy.stats library.
import scipy.stats as stats
# Example Data (Does not need to be the same size or normally distributed)
group_a = [12, 14, 15, 12, 18, 22, 14]
group_b = [10, 11, 14, 10, 15, 9]
# Run the Mann-Whitney U test
u_stat, p_value = stats.mannwhitneyu(group_a, group_b, alternative='two-sided')
print(f"U-Statistic: {u_stat}")
print(f"P-Value: {p_value:.4f}")
How to Run a Mann-Whitney U Test in R
In R, the Mann-Whitney U test is run using the built-in wilcox.test() function. (Because Mann-Whitney U and the Wilcoxon Rank-Sum test are mathematically identical, R combines them into one function).
# Example Data
group_a <- c(12, 14, 15, 12, 18, 22, 14)
group_b <- c(10, 11, 14, 10, 15, 9)
# Run the test
result <- wilcox.test(group_a, group_b, paired = FALSE)
# Print the results
print(result)
(Tip: To skip the coding entirely, you can paste your raw data directly into our free Mann-Whitney U Calculator.)
