Reputation: 1
I have a gene interaction data like down below. Any idea how to make an adjacency matrix from this data?
Upvotes: 0
Views: 361
Reputation: 2261
Example data:
cat_type = pd.CategoricalDtype(list("abcdef"))
df = pd.DataFrame(
{
"node1":["a", "b", "a", "c", "b", "f"],
"node2":["b", "d", "c", "e", "f", "e"],
}
).astype(cat_type)
df
looks like this:
node1 node2
0 a b
1 b d
2 a c
3 c e
4 b f
5 f e
Solution
adj_mat = pd.crosstab(df["node1"], df["node2"], dropna=False)
results in a dataframe:
a b c d e f
a 0 1 1 0 0 0
b 0 0 0 1 0 1
c 0 0 0 0 1 0
d 0 0 0 0 0 0
e 0 0 0 0 0 0
f 0 0 0 0 1 0
If you need it symmetrical around the diagonal then the following will give you a boolean result
adj_mat.transpose() + adj_mat > 0
which you can then convert to integer with .astype(int)
if required
Upvotes: 1