Skip to content

Instantly share code, notes, and snippets.


codecademydev/ Secret

Created Jan 30, 2021
What would you like to do?
Codecademy export
import numpy as np
import pandas as pd
import codecademylib3
from scipy.stats import chi2_contingency
npi = pd.read_csv("npi_sample.csv")
def contingency_analisis(dataframe, column1, column2):
frequency = pd.crosstab(npi[column1], npi[column2])
#print(frequency, '\n')
proportion = frequency / len(dataframe)
#print(proportion, '\n')
chi2, pval, dof, expected = chi2_contingency(frequency)
#print('The chi2 is:', str(chi2))
#print('The pval is:', str(pval))
#print('The expected array is:')
return dataframe, chi2
chi2_dict = {}
columns = npi.columns
for column in columns:
for i in range(len(columns)):
if column == columns[i]:
key = column, columns[i]
dataframe, chi2 = contingency_analisis(npi, column, columns[i])
chi2_dict[key] = chi2
#contingency_analisis(npi, 'leader', 'authority')
index = list(range(len(chi2_dict)))
df = pd.DataFrame(chi2_dict, index = [0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment