Contingency table in Python

Crosstalk is one method for examining two or even more variables. It's basically counting the amount between two or more categorical variables.

To get loan data, click here .

Loading Libraries

import numpy as np

import pandas as pd

import matplotlib as plt

Loading data

data = pd.read_csv ( "loan_status.csv" )

  

print (data.head ( 10 ))

Output:

Describe the data

Output:

Data Information

data.describe ()

data.info ()

Output:

< p> Data Types

# object / attribute data types
# in data
data.dtypes

Output:

Code # 1: A contingency table showing the relationship between grades and credit status.

data_crosstab = pd.crosstab (data [ 'grade' ],

data [ 'loan_status' ], 

  margins = False )

print (data_crosstab)

Output:

Code # 2: A contingency table showing the relationship between purpose and credit status.

data_crosstab = pd.crosstab (data [ 'purpose' ], 

data [ 'loan_status' ], < / p>

margins = False )

print ( data_crosstab)

Output:

Code # 3: A contingency table showing the relationship between classes + purpose and status of the loan.

data_crosstab = pd.crosstab ([data.grade, data.purpose], 

data.loan_status, margins = False )

print (data_crosstab)

Output:

Like in code, contingency tables give clear correlation values ​​between two or more variables. Thus, it is much more useful to understand the data for further information extraction. 
,





Get Solution for free from DataCamp guru