1

I have a sample dataset as below

  Phillippines  Indonesia   Malta       India
0   Error Free  Error Free  Defective   Error Free
1   Error Free  Error Free  Error Free  Defective
2   Error Free  Defective   Defective   Error Free
3   Error Free  Error Free  Error Free  Error Free
4   Error Free  Error Free  Defective   Error Free

I want to make a contingency table with row index as Defective, Error Free and column index as Phillippines, Indonesia, Malta, India and data as their corresponding value counts. Thanks in advance.

1 Answers1

0

Hi think you are looking for below result.

data_crosstab = pd.crosstab([df['Phillippines'], df['Indonesia'], df['Malta'], df['India']], 'result')

above code will give you the following result.

enter image description here

Hope it will solve!!

  • Hi.. Thanks for answering, but I am looking for contingency table. – Vaitybharati Mar 10 '21 at 13:58
  • Not understood it is a contingency table. give me sample output if you can or what is wrong with above. – yuvraj singh Mar 10 '21 at 14:08
  • Hi.. I want contingency table like this one for example http://www.mathandstatistics.com/wp-content/uploads/2014/06/EyeColorContTable.jpg – Vaitybharati Mar 10 '21 at 14:20
  • HI @Vaitybharati please take look this one I think you are looking for this. maybe you need to change your data like he explains. https://chrisalbon.com/python/data_wrangling/pandas_crosstabs/ – yuvraj singh Mar 10 '21 at 14:40
  • I was able to find solution using value_counts() pandas code. But had to individually apply it to all columns and then prepare contingency table in array format.. I am looking for direct code..Thanks – Vaitybharati Mar 10 '21 at 15:10