Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate the contingency table for 2 categorical variables in a Pandas DataFrame
# Install xarray first
# col1 & col2: hold the categorical variables
# col3: a column which has a value in all rows
contingency_table = ((df.groupby(['col1', 'col2']).count().to_xarray()['col3']).values).tolist()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment