title: How to perform chisquare tests on rows of pandas dataframes?
body: <p>I have a dataframe <strong>df</strong> of the form</p>
  
  <pre><code>          class_1_frequency    class_2_frequency
  group_1          20                    10
  group_2          60                    25 
  ..
  group_n          50                    15 
  </code></pre>
  
  <p>Suppose class_1 has a total of 70 members and class_2 has 30.</p>
  
  <p>For each row (group_1, group_2,..group_n) I want to create contingency tables (preferably dynamically) and then carry out a <a href="https://en.wikipedia.org/wiki/Chi-squared_test" rel="nofollow noreferrer">chisquare test</a> to evaluate p-values.</p>
  
  <p>For example, for group_1, the contingency table under the hood would look like:</p>
  
  <pre><code>                   class_1      class_2
  group_1_present      20           10
  group_1_absent     70-20         30-10
  </code></pre>
  
  <p>Also, I know <a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.chi2_contingency.html" rel="nofollow noreferrer">scipy.stats.chi2_contingency()</a> is the appropriate function for chisquare, but I am not able to apply it to my context. I have looked at previously discussed questions such as: <a href="https://stackoverflow.com/questions/40667736/calculate-chi-sqaure-between-pairs-of-columns">here</a> and <a href="https://stackoverflow.com/questions/43963606/python-pandas-chi-squared-test-of-independence">here</a>.</p>
  
  <p>What is the most efficient way to achieve this?  </p>