Question on chi-squared test formula in Biodiversity Capstone Project

BIODIVERSITY CAPSTONE PROJECT - INVESTIGATING PROTECTED SPECIES Chi-Squared Test for Significance

I got stuck, so I referred to the solution code for instruction #2 involving the chi-squared test. Here is the answer:

import codecademylib
import pandas as pd
from matplotlib import pyplot as plt
from scipy.stats import chi2_contingency

# Instruction 1
contingency = [[30, 146], [75, 413]]

# Instruction 2
pval = chi2_contingency(contingency)[1]

print(pval)

My question: For instruction 2, what does the [1] at the end of the formula do? Why is it needed? The code doesn’t run without it. Any help would be greatly appreciated.

-Michael

chi2_contingency returns a list with 4 values: chi2, p, dof, expected… in this order.
this exercise expect you to get the pvalue. so, the index 1 from the return.

chi2_contingency[0] = chi2
chi2_contingency[1] = p
chi2_contingency[2] = dof
chi2_contingency[3] = expected

reference

1 Like

Thank you. Much appreciated.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.