简体   繁体   中英

Pandas: How to convert a one-hot-encoded dataframe to an adjacency matrix?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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