从邻接表创建邻接矩阵

Create adjacency matrix from adjacency list

我有下一个有两列的 DF

A   x
A   y
A   z
B   x
B   w
C   x
C   w
C   i

我想生成这样的邻接矩阵(计算交集)

    A   B   C
A   0   1   2
B   1   0   2
C   2   2   0

我有下一个代码但不起作用:

import pandas as pd

df = pd.read_csv('lista.csv')
drugs = pd.read_csv('drugs.csv')
drugs = drugs['Drug'].tolist()
df = pd.crosstab(df.Drug, df.Gene)
df = df.reindex(index=drugs, columns=drugs)

如何获取邻接矩阵?

谢谢

尝试在第 2 列上自行合并,然后交叉表:

s = df.merge(df,on='col2').query('col1_x != col1_y')
pd.crosstab(s['col1_x'], s['col1_y'])

输出:

col1_y  A  B  C
col1_x         
A       0  1  1
B       1  0  2
C       1  2  0

输入:

>>> drugs
  Drug Gene
0    A    x
1    A    y
2    A    z
3    B    x
4    B    w
5    C    x
6    C    w
7    C    i

合并 crosstab 之前的基因并用零填充对角线

df = pd.merge(drugs, drugs, on="Gene")
df = pd.crosstab(df["Drug_x"], df["Drug_y"])
np.fill_diagonal(df.values, 0)

输出:

>>> df
Drug_y  A  B  C
Drug_x
A       0  1  1
B       1  0  2
C       1  2  0