In this section we will learn how to create cross table in python pandas ( 2 way cross table or 3 way cross table or contingency table) with example. We will learn how to create.
- 2 way cross table or contingency table in python pandas
- 3 way cross table or contingency table in python pandas
Create dataframe :
import pandas as pd import numpy as np #Create a DataFrame d = { 'Name':['Alisa','Bobby','Cathrine','Alisa','Bobby','Cathrine', 'Alisa','Bobby','Cathrine','Alisa','Bobby','Cathrine'], 'Exam':['Semester 1','Semester 1','Semester 1','Semester 1','Semester 1','Semester 1', 'Semester 2','Semester 2','Semester 2','Semester 2','Semester 2','Semester 2'], 'Subject':['Mathematics','Mathematics','Mathematics','Science','Science','Science', 'Mathematics','Mathematics','Mathematics','Science','Science','Science'], 'Result':['Pass','Pass','Fail','Pass','Fail','Pass','Pass','Fail','Fail','Pass','Pass','Fail']} df = pd.DataFrame(d,columns=['Name','Exam','Subject','Result']) df
so the resultant dataframe will be
2 Way Cross table in python pandas:
We will calculate the cross table of subject and result as shown below
# 2 way cross table pd.crosstab(df.Subject, df.Result,margins=True)
margin=True displays the row wise and column wise sum of the cross table so the output will be
Two way frequency table or cross table: Get proportion using crosstab() function:
STEP 1 : Rename to get row total and column total
To get the over all proportion lets first rename the two way cross table. The columns and index of the two way cross table is renamed to get the row total and column total as shown below
#### Rename the index and columns my_crosstab.columns = ["Fail" , "Pass" , "rowtotal"] my_crosstab.index= ["Mathematics","Science","coltotal"] my_crosstab
so the renamed frequency table will be
Step 2: Get over all proportion of the frequency table
the cross table is divided by row total and column total to get the proportion as shown below
#### Get the over all proportion my_crosstab/my_crosstab.ix["coltotal","rowtotal"]
so the cross table with proportion will be
Two way frequency table : Get column wise proportion using crosstab() function
the cross table is divided by column total to get the column wise proportion as shown below
#### Get the column proportion my_crosstab/my_crosstab.ix["coltotal"]
so the cross table with column wise proportion will be
Two way frequency table : Get row wise proportion using crosstab() function
the cross table is divided by row total to get the row wise proportion as shown below
#### Get the row proportion my_crosstab.div(my_crosstab["rowtotal"],axis=0)
so the row table with row wise proportion will be
3 Way Cross table in python pandas:
We will calculate the cross table of subject, Exam and result as shown below
# 3 way cross table pd.crosstab([df.Subject, df.Exam],df.Result, margins=True)
the result will be
Other related topics:
- Get the percentage of a column in pandas python
- Cumulative percentage of a column in pandas python
- Cumulative sum of a column in pandas python
- Difference of two columns in pandas dataframe – python
- Sum of two or more columns of pandas dataframe in python
- Set difference of two dataframe in Pandas python
- Intersection of two dataframe in Pandas python
to read on more you refer the documentation.