我有一个像这样的数据帧:
ID | Node 1 | Node 2 | Node 3
a | 1 | 0 | 1
b | 0 | 1 | 1
c | 1 | 0 | 0
d | 1 | 1 | 1
e | 0 | 1 | 1
我想更改它,以便我可以将其转换为网络图表,其中节点之间的连接是指示两者的ID的次数:
Node A | Node B | Weight |
Node 1 | Node 2 | 1 |
Node 1 | Node 3 | 2 |
Node 2 | Node 3 | 3 |
最佳答案 建于
Tai’s solution,
你可以使用获得所需的DataFrame
import numpy as np
import pandas as pd
def get_weights(df):
df2 = df.filter(regex='Node')
nodes = df2.columns
arr = df2.values
m = np.dot(arr.T, arr).astype(float)
idx = np.tril_indices(m.shape[0])
m[idx] = np.nan
result = pd.DataFrame(m, columns=nodes, index=nodes)
result = result.stack()
result = result.astype(int)
result = result.reset_index()
result.columns = ['Node A', 'Node B', 'Weights']
return result
df = pd.DataFrame({'ID': ['a', 'b', 'c', 'd', 'e'],
'Node 1': [1, 0, 1, 0, 0],
'Node 2': [0, 1, 0, 1, 1],
'Node 3': [1, 1, 0, 1, 1]})
result = get_weights(df)
print(result)
产量
Node A Node B Weight
0 Node 1 Node 2 1
1 Node 1 Node 3 2
2 Node 2 Node 3 3