I have the following numpy array:
import numpy as np
pair_array = np.array([(205, 254), (205, 382), (254, 382), (18, 69), (205, 382),
(31
You could create a data frame of the appropriate size with zeros beforehand and just increment the appropriate cells by looping over the pairs:
import numpy as np
import pandas as pd
pair_array = np.array([(205, 254), (205, 382), (254, 382), (18, 69), (205, 382),
(31, 183), (31, 267), (31, 82), (183, 267), (183, 382)])
vals = sorted(set(pair_array.flatten()))
n = len(vals)
df = pd.DataFrame(np.zeros((n, n), dtype=np.int), columns=vals, index=vals)
for r, c in pair_array:
df.at[r, c] += 1
df.at[c, r] += 1
print(df)
Output:
18 31 69 82 183 205 254 267 382
18 0 0 1 0 0 0 0 0 0
31 0 0 0 1 1 0 0 1 0
69 1 0 0 0 0 0 0 0 0
82 0 1 0 0 0 0 0 0 0
183 0 1 0 0 0 0 0 1 1
205 0 0 0 0 0 0 1 0 2
254 0 0 0 0 0 1 0 0 1
267 0 1 0 0 1 0 0 0 0
382 0 0 0 0 1 2 1 0 0