pairwise-test-python provides simple helper functions for running pairwise
statistical tests on categorical data stored in a pandas DataFrame.
The package currently includes:
chisquare(): pairwise chi-square tests across all combinations of a fixed grouping variablefisher(): pairwise Fisher's exact tests across all combinations of a fixed grouping variable
Both functions return the raw p-value and a Bonferroni-corrected p-value based on the total number of pairwise tests performed.
pandasscipy
import pandas as pd
from pairwise import chisquare, fisher
df = pd.DataFrame(
{
"Group": ["A", "A", "B", "B", "C", "C", "A", "B", "C"],
"Sex": ["M", "F", "M", "F", "M", "F", "M", "M", "F"],
}
)
chi2_results = chisquare(df, "Group", "Sex")
fisher_results = fisher(df, "Group", "Sex")
print(chi2_results)
print(fisher_results)Each function returns a pandas DataFrame with these columns:
Variable1Variable2p-valuebonferroni p-value
The Bonferroni-corrected p-value is computed as:
min(p_value * number_of_tests, 1.0)where number_of_tests is the number of pairwise comparisons generated from
the unique values in the fixed variable column.
col1should be the fixed grouping variable.col2should be the categorical outcome variable.- The functions also print the result of each pairwise comparison to the console.
- Use
help(chisquare)orhelp(fisher)for function-level documentation.