关系对转换为邻接矩阵

import pandas as pd
import numpy as np
 
#导入你的数据
data = pd.read_csv('./yourdata.csv')
 
vals = np.unique(data[['origin_x', 'origin_y']])  # 同时取出两列,作为节点
df = pd.DataFrame(0, index=vals, columns=vals)
f = df.index.get_indexer
df.values[f(data.origin_x), f(data.origin_y)] = 1 #如果使用权重值,则改为:data.weight列
print(df)

 

import pandas as pd
import numpy as np
 
#导入你的数据
data = pd.read_csv('./yourdata.csv')
 
#转换为邻接矩阵用pivot()
df = data.pivot(index='node1',columns='node2',values='weight') 
    #index为行索引,columns为列索引,values是矩阵中填充的值
df = df.reset_index()
df.fillna(0,inplace=True)
print(df)

参考:https://blog.csdn.net/Zhaoli_Amiee/article/details/127988749

posted @ 2023-12-01 15:33  cup_leo  阅读(30)  评论(0编辑  收藏  举报