Skip to content

Instantly share code, notes, and snippets.

@otayeby
Last active March 18, 2021 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otayeby/6ce194a94ef414503751d27f5226a116 to your computer and use it in GitHub Desktop.
Save otayeby/6ce194a94ef414503751d27f5226a116 to your computer and use it in GitHub Desktop.
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