<jupyter_start><jupyter_text>Crosstab Tutorial<jupyter_code>import pandas as pd
df = pd.read_excel("survey.xls")
df
df['Nationality'].value_counts()
pd.crosstab(df.Nationality, df.Handedness)
pd.crosstab(df.Sex, df.Handedness)<jupyter_output><empty_output><jupyter_text>Margins<jupyter_code>pd.crosstab(df.Sex, df.Handedness, margins=True)<jupyter_output><empty_output><jupyter_text>Multi Index Column and Rows<jupyter_code>pd.crosstab(df.Sex, [df.Handedness,df.Nationality], margins=True)
pd.crosstab([df.Nationality, df.Sex], [df.Handedness], margins=True)<jupyter_output><empty_output><jupyter_text>Normalize- shows percentage<jupyter_code>pd.crosstab(df.Sex, df.Handedness, normalize='index')<jupyter_output><empty_output><jupyter_text>Aggfunc and Values<jupyter_code>import numpy as np
pd.crosstab(df.Sex, df.Handedness, values=df.Age, aggfunc=np.average)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1404494382022472, 'fraction_numerical': 0.0, 'mean_word_length': 12.102941176470589, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '259034', 'license': 'CNRI-Python', 'n_tokens_mistral': 386, 'n_tokens_neox': 356, 'n_words': 38, 'path': '8_Grouping and Aggregating/2_Crosstab/1_Survey/pandas_crosstab.ipynb', 'repo_name': 'sureshmecad/Pandas', 'size': '890'}