# ChiSquare test Code in Python

I was trying to write a code from scratch for Chi-square test.This is the code that I had written in python using pandas.I had a doubt whether the code can produce the desired output or can be written in a more compacted way.

import pandas as pd
from scipy import chisqprob
from scipy import stats

def chi_square_of_df_cols(main_frame,column1,column2):
grouped_data=pd.DataFrame(main_frame.groupby([column1,column2])[column1].count())
grouped_data.columns=["count"]
grouped_data.reset_index(inplace=True)
pivoted_data=grouped_data.pivot(column1,column2,"count")
pivoted_data.fillna(0,inplace=True)
frame = pivoted_data.copy(deep="True")
cols=pivoted_data.columns
sum_row = {col: pivoted_data[col].sum() for col in pivoted_data}
dof=(len(pivoted_data.columns)*len(pivoted_data))-1
pivoted_data["Total_row"] = pivoted_data.sum(axis=1)
grid_total=pivoted_data["Total_row"].sum(axis=0)
for col in cols:
for i in pivoted_data.index:
frame.ix[i,col]= (pivoted_data.ix[i,"Total_row"])*(sum_row[col])/grid_total

sum1=0
for col in cols:
for i in pivoted_data.index:
sum1+=(pivoted_data.ix[i,col]-frame.ix[i,col])**2/frame.ix[i,col]

p_value=chisqprob(sum1,dof)

return p_value