Reputation: 43
Can someone tell me how to generate an interaction matrix from a dataframe as follows?
Input:
cd1 = [True, False, True, False, False]
cd2 = [False, True, True, False, False]
cd3 = [True, False, False, True, True]
df = pd.DataFrame({"cd1":cd1, "cd2": cd2, "cd3": cd3})
I want an output that looks like this
cd1 cd2 cd3
cd1 2 1 1
cd2 1 2 0
cd3 1 0 3
Upvotes: 2
Views: 1595
Reputation: 323226
Just one more option matmul
from numpy
pd.DataFrame(np.matmul(df.T.astype(int),df.astype(int)),columns=df.columns,index=df.columns)
Out[283]:
cd1 cd2 cd3
cd1 2 1 1
cd2 1 2 0
cd3 1 0 3
Upvotes: 1
Reputation: 402493
Convert df
to int
type and compute the matrix dot
product:
df.astype(int).T.dot(df)
cd1 cd2 cd3
cd1 2 1 1
cd2 1 2 0
cd3 1 0 3
Upvotes: 3