Title: Selecting part of contingency table in Python pandas
Tags: python;pandas
Question: Problem
I am working on a project which is trying to detect the association of sexism and genders existent in the dataset. Part of the dataset looks like
male  female  sexism?
0      0       1        1
1      1       1        0
2      0       1        1
3      1       1        1
4      1       1        0
..   ...     ...      ...
Now I would like to get the statistics of this dataset, which looks like
sexism   non-sexism
male   xxx      xxx
female xxx      xxx
As much as I know I could do something similar to this using pd.crosstab (see following code chunk), but it is not as straightforward as I anticipated
pd.crosstab([[df["male"], df["sexism?"]], [[df["female"], df["sexism?"]])
Could someone help me? Thank you in advance!
Comment: What you want to do , you have 4 D for male and female , and you only want two D ?

Here is the accepted answer: I think you need mean to see the percentage
df.groupby('sexism?').mean().T
Out[19]:
sexism?    0         1
male     1.0  0.333333
female   1.0  1.000000
Update
df.groupby('sexism?').sum().T
Out[9]:
sexism?  0  1
male     2  1
female   2  3
Comment for this answer: Thank you for your help! But what if what I want is the **number** (instead of percentage) of each `xxx`. Say the `xxx` at the intersection of `male` and `sexism` means number of rows when `male=1` and `sexism?=1` while `xxx` at `male` and `non-sexism` means `male=1` and `sexism?=0`


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:11/16/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><pandas>', 'Title': 'Selecting part of contingency table in Python pandas', 'ViewCount': 40, 'id': 58886972, 'user_AboutMe': None, 'user_AccountId': 10566788.0, 'user_DisplayName': 'Mr.Robot', 'user_Reputation': 322.0, 'user_Views': 109.0, 'user_id': 7784797.0}}