Reputation: 2655
I have a data frame df, of the form:
Col1 Col2 Col3
0 0 1 0
1 1 1 0
2 0 1 1
3 1 1 0
I need a new df of the form:
Col1 Col2 Col3
Col1 0 2 0
Col2 2 0 1
Col3 0 1 0
Basically the values represent the co-occurrences of two given columns for all rows.
How do I go about this?
Upvotes: 3
Views: 496
Reputation: 221574
Simply leverage matrix-multiplication
there -
In [21]: df_out = df.T.dot(df)
In [22]: np.fill_diagonal(df_out.values, 0)
In [23]: df_out
Out[23]:
Col1 Col2 Col3
Col1 0 2 0
Col2 2 0 1
Col3 0 1 0
Upvotes: 4