How do I "merge/add" 2 confusion matrix dataframes that have the same columns and rows using Python Pandas?

Viewed 197

I am looking for an efficient solution to do the following using Python Pandas dataframes:

                 Iris-setosa  Iris-versicolor  Iris-virginica
Iris-setosa                4                0               0
Iris-versicolor            0                1               3
Iris-virginica             0                0               7

merge/sum with:

                 Iris-setosa  Iris-versicolor  Iris-virginica
Iris-setosa                6                0               0
Iris-versicolor            0                2               2
Iris-virginica             0                0               5

For the end result of:

                 Iris-setosa  Iris-versicolor  Iris-virginica
Iris-setosa               10                0               0
Iris-versicolor            0                3               5
Iris-virginica             0                0               12

My actual confusion matrices are much larger so I am looking for an efficient solution.

I have tried brute force going through each index of each matrix and summing them together, but that is very inelegant and I would like to learn a faster way.

2 Answers

IIUC, we can use concat and groupby along the index using sum to get total of the values.

df3 = pd.concat([df1,df2]).groupby(level=0).sum()
print(df3)
                     Iris-setosa  Iris-versicolor  Iris-virginica
Iris-setosa               10                0               0
Iris-versicolor            0                3               5
Iris-virginica             0                0              12

if the indices are absolutely aligned we can just sum the entire dataframe.

df3 = df1 + df2
print(df3)

                 Iris-setosa  Iris-versicolor  Iris-virginica
Iris-setosa               10                0               0
Iris-versicolor            0                3               5
Iris-virginica             0                0              12

Unless I am missing something, y not just do +:

df3 = df1 + df2

For the same given indices, I tested it like this:

print(df1)

                 Iris-setosa  Iris-versicolor  Iris-virginica
Iris-setosa                6                0               0
Iris-versicolor            0                2               2
Iris-virginica             0                0               5

print(df2)

                 Iris-setosa  Iris-versicolor  Iris-virginica
Iris-setosa                4                0               0
Iris-versicolor            0                1               3
Iris-virginica             0                0               7


df3 = df1 + df2

print(df3)

                 Iris-setosa  Iris-versicolor  Iris-virginica
Iris-setosa               10                0               0
Iris-versicolor            0                3               5
Iris-virginica             0                0              12
Related