-1

all could you please help how can I to check if the types of food and the anonymity of entries are independent or correlated by using a Chi-square test? I have to find the correlation between group column and manufacturer. The problem is here is fgroup and manufacturer columns re object type, and I get different errors: for example :

from scipy.stats import chisquare
chisquare(df['fgroup'], df['manufacurer  '])

error: unsupported operand type(s) for -: 'str' and 'NoneType' data is here:enter image description here Thanks in advance:

Mr_12
  • 23
  • 5

1 Answers1

1

The easiest way is most likely to tabulate them first, using pd.crosstab() and use chi2_contingency :

import pandas as pd
from scipy.stats import chi2_contingency
import numpy as np

np.random.seed(111)
df = pd.DataFrame({'fgroup':np.random.choice(['f1','f2','f3'],50),
                   'manufacturer':np.random.choice(['m1','m2'],50)})

chi2_contingency(pd.crosstab(df['fgroup'],df['manufacturer']))

See here for what the output values mean.

BoomBoxBoy
  • 1,770
  • 1
  • 5
  • 23
StupidWolf
  • 45,075
  • 17
  • 40
  • 72