Coderhouse goes global

🌍

Read more

What Is a Chi-Square Test? How Data Analysts Test Relationships Between Categories

AI Engineering

Build and ship real applications with LLMs, agents and automation.

8 weeks · Live online · View course →

Data Analytics

Go from zero to job-ready analyst with SQL, Python and BI tools.

10 weeks · Live online · View course →

Dan Patiño

AI Strategy & Innovation at Coderhouse

Data

What Is a Chi-Square Test? How Data Analysts Test Relationships Between Categories

What Is a Chi-Square Test? How Data Analysts Test Relationships Between Categories

A chi-square test is a statistical test that checks whether the differences you see in counts of categorical data are bigger than what random chance would produce. Analysts use it to answer questions like: does conversion depend on which landing page a visitor saw? Is the plan customers choose related to their country? Do support tickets follow the distribution we expected across categories? Instead of comparing averages, it compares observed counts with the counts you would expect if there were no relationship.

It matters because a lot of business data is categorical: yes or no, plan A or plan B, channel, region, device. Looking at a table of percentages can be misleading, especially with small samples. The chi-square test gives you a disciplined way to decide whether a pattern is likely real before you build a recommendation on it. It is one of the most common tools in hypothesis testing for analysts.

The two main types of chi-square tests

Chi-square test of independence

Used with a contingency table that crosses two categorical variables, for example variant (A or B) by outcome (converted or not). The null hypothesis is that the two variables are independent: the outcome does not depend on the variant.

Chi-square goodness-of-fit test

Used with one categorical variable to check whether observed counts match an expected distribution. For example, if you expect tickets to split evenly across five weekdays, the test checks whether the real split is consistent with that. The NIST/SEMATECH e-Handbook of Statistical Methods describes the goodness-of-fit version and its assumptions.

How the chi-square statistic is calculated

The logic is the same for both types:

  1. Build the table of observed counts (O) for each category or cell.

  2. Calculate expected counts (E). In a test of independence, each expected count is (row total × column total) / grand total.

  3. Compute the statistic: for each cell, take (O − E)² / E, then add all cells together.

  4. Find the degrees of freedom. For independence it is (rows − 1) × (columns − 1). For goodness of fit it is the number of categories minus 1.

  5. Get the p-value from the chi-square distribution with those degrees of freedom and compare it with your significance level, usually 0.05.

A worked example: did the new landing page convert better?

Suppose 200 visitors saw page A and 30 converted, while 200 visitors saw page B and 50 converted. The observed table is:

  • Page A: 30 converted, 170 did not.

  • Page B: 50 converted, 150 did not.

In total, 80 of 400 visitors converted. If the page made no difference, each page would expect 200 × 80 / 400 = 40 conversions and 160 non-conversions. The statistic is:

(30 − 40)²/40 + (50 − 40)²/40 + (170 − 160)²/160 + (150 − 160)²/160 = 2.5 + 2.5 + 0.625 + 0.625 = 6.25

With a 2 × 2 table there is 1 degree of freedom. The critical value at a 0.05 significance level is about 3.84, and 6.25 corresponds to a p-value of about 0.012. Because it is below 0.05, you reject the null hypothesis: the difference in conversion is unlikely to be explained by chance alone.

Running a chi-square test in Python

In practice, analysts rarely calculate it by hand. In Python, scipy.stats.chi2_contingency takes the observed table and returns the statistic, p-value, degrees of freedom and expected counts. The SciPy documentation notes that for tables with one degree of freedom it applies Yates' continuity correction by default. In the example above, passing correction=False reproduces the 6.25 statistic, while the default corrected version returns about 5.64 with a p-value near 0.018. Both lead to the same conclusion here, but you should report which one you used.

Spreadsheets also support it: Excel and Google Sheets include a CHISQ.TEST function that returns the p-value from an observed range and an expected range.

Assumptions and common mistakes

  • Use counts, not percentages. The test needs raw frequencies. Running it on percentages or averages gives meaningless results.

  • Observations must be independent. Each person or event should appear in one cell only. Counting the same user several times breaks the test.

  • Expected counts should not be too small. A common rule of thumb is that expected counts should be at least 5 in each cell. With small samples, analysts often use Fisher's exact test for 2 × 2 tables.

  • Significance is not size. A tiny p-value with millions of rows can come from a difference too small to matter. Report the actual rates and, when possible, a confidence interval for the difference.

  • It shows association, not cause. In observational data, a relationship between region and plan choice does not mean region causes the choice. Randomized experiments are what support causal claims.

When analysts use the chi-square test

  • A/B tests with a binary outcome, such as signup or purchase.

  • Survey analysis, for example satisfaction level by customer segment.

  • Checking whether traffic or orders are distributed as expected across days, regions or channels.

  • Data quality checks, such as detecting a shift in category distribution after a pipeline change.

FAQ

Is a chi-square test the same as a t-test?

No. A t-test compares the means of a numeric variable between groups. A chi-square test compares counts across categories.

Can I use it with more than two groups?

Yes. The test of independence works with any number of rows and columns. If it is significant, follow up to see which cells drive the difference.

What does a large chi-square value mean?

That observed counts are far from expected counts. Whether that is significant depends on the degrees of freedom, which is why you always read it together with the p-value.

Keep learning with Coderhouse

Turning tables of counts into reliable decisions is part of an analyst's daily work. Explore the path that fits your goals:

  • Data Analytics Course — learn SQL, Python, statistics and dashboards to test hypotheses with real business data.

  • Forward Deployed AI Engineering Course — learn to build, evaluate and deploy production AI systems, where statistical checks help validate model behavior.

  • GTM Engineer Course (waitlist) — join the waitlist for early access to the program focused on building AI-powered go-to-market systems, including outbound and conversion experiments.

Dan Patiño

I'm Dan Patiño, head of AI Strategy & Innovation at Coderhouse. My day-to-day work involves merging the tactical management of e-commerce (CRO, Email Marketing and SEO) with the development of disruptive solutions. I specialize in building internal AI-powered apps to automate tasks and boost innovation within the team. I firmly believe that technology is strategy's best ally. To dive deeper into my professional journey, I'll be waiting for you on my LinkedIn profile.