简体   繁体   English

Pandas:从交叉表到计数表

[英]Pandas: from crosstab to a count table

I have a table of 0's and 1's generated from pd.crosstab()我有一个从pd.crosstab()生成的 0 和 1 表

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我想将它转换为一个表格,其中的列和行都是成分,并且T[i,j] = #number of recipes that have both ingredients, i and j

In our example, T[banana, chocolate] = 1 and T[banana, tuna] = 0在我们的示例中, T[banana, chocolate] = 1T[banana, tuna] = 0

How to do that?怎么做?

You can do matrix multiplication :你可以做 矩阵乘法

df.T @ df

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM