Search code examples
pythonpandasconfusion-matrix

Counting items by different conditions in rows in Python Pandas


I am trying to create a confusion matrix of an experiment.

So the dataset is like this;

    Responses   Condition
3    1             R
4    1             R
6    1             R
7    1             R
8   -1             R
9   -1             N
10  -1             N
11  -1             N
12  -1             R
13   1             R

I want to categorize four different conditions; and I want to count each one of the conditions.

1 & N, -1 & N, 1 & R, -1 & R

I want to count each one of those situations in the dataframe.

I have tried to use .itertuples , but I don't know how to use it with 2 parameters.


Solution

  • Try this:

    df.groupby(['Condition','Responses']).size().unstack(fill_value=0).stack()
    Condition  Responses
    N          -1           3
                1           0
    R          -1           2
                1           5
    dtype: int64
    

    Or

    pd.crosstab(df.Condition,df.Responses)
    Responses  -1   1
    Condition        
    N           3   0
    R           2   5