One way could be to build a graph using NetworkX and obtain the adjacency matrix directly as a dataframe with nx.to_pandas_adjacency. To account for the co-occurrences of the edges in the graph, we can create a nx.MultiGraph, which allows for multiple edges connecting the same pair of nodes:
import networkx as nx
G = nx.from_edgelist(pair_array, create_using=nx.MultiGraph)
nx.to_pandas_adjacency(G, nodelist=sorted(G.nodes()), dtype="int")
18 31 69 183 205 254 267 382
18 0 0 1 0 0 0 0 0
31 0 0 0 1 0 0 1 1
69 1 0 0 0 0 0 0 0
183 0 1 0 0 0 0 1 1
205 0 0 0 0 0 1 0 2
254 0 0 0 0 1 0 0 1
267 0 1 0 1 0 0 0 0
382 0 1 0 1 2 1 0 0
Building a NetworkX graph, will also enable to create an adjacency matrix or another depending on the behaviour we expect. We can either create it using a:
nx.Graph: If we want to set to1both entries(x,y)and (y,x) for a(x,y)(or(y,x)) edge. This will hence produce a symmetric adjacency matrixnx.DiGraph: If(x,y)should only set the(x,y)the entry to1nx.MultiGraph: For the same behaviour as anx.Graphbut accounting for edge co-occurrencesnx.MultiDiGraph: For the same behaviour as anx.DiGraphbut also accounting for edge co-occurrences