Last active
March 18, 2021 00:48
-
-
Save otayeby/6ce194a94ef414503751d27f5226a116 to your computer and use it in GitHub Desktop.
Generate the contingency table for 2 categorical variables in a Pandas DataFrame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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