Elimination
Elimination

Reputation: 2723

Pandas: from crosstab to a count table

I have a table of 0's and 1's generated from pd.crosstab()

A row is a recipe and the columns are ingredients. So for example we can have:

              banana mushrooms ... chocolate tuna
banana-split  1      0         ... 1         0

I'd like to transform it to a table where the columns and rows are both the ingredients and T[i,j] = #number of recipes that has both ingredients, i and j

In our example, T[banana, chocolate] = 1 and T[banana, tuna] = 0

How to do that?

Upvotes: 0

Views: 117

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150745

You can do matrix multiplication:

df.T @ df

Upvotes: 4

Related Questions