Contingency table produces 2*1 instead of 2*2 with crosstab

20 Views Asked by At

I have created a contingency table with crosstab that provides frequency of each combination of rater 1 and rater 2 ratings, with '0' and '1' as the row and column labels from the below data frame.

import pandas as pd
import numpy as np

# create data frame with index as items 1 to 13
df = pd.DataFrame(index=['item ' + str(i) for i in range(1, 14)])

# add column for rater 1 with random 0s and 1s
df['rater 1'] = np.random.randint(0, 2, size=13)

# add column for rater 2 with all 1s
df['rater 2'] = np.ones(13, dtype=int)

df

[df][1]

|   |rater 1|rater 2|
|:---:|:---:|:---:|
|item 1 |0  |1|
|item 2 |1  |1|
|item 3 |1  |1|
|item 4 |1  |1|
|item 5 |1  |1|
|item 6 |1  |1|
|item 7 |1  |1|
|item 8 |0  |1|
|item 9 |1  |1|
|item 10|1  |1|
|item 11|0  |1|
|item 12|0  |1|
|item 13|1  |1|

The code i used for this is

# create cross tab for rater 1 and rater 2
cross_tab = pd.crosstab(df['rater 1'], df['rater 2'])

print(cross_tab)

Output is ;

output file

This is not the expected output. i expected output to be below

||rater 2   |0  |1|
|:---:|:---:|:---:|
|rater 1    |   | |
|0  | 0 | 4|
|1  | 0 | 9 |

required output

Can someone help with a walk around this, need to customize this to a 2 x 2 data Frame

0

There are 0 best solutions below