我有一个球员列表,我试图找到这些球员成对出现的矩阵。
players = [Player52, Player48,Player41,Player42,Player52,Player48]
matrix =
Player52 Player48 Player41 Player42
Player52 0 2 0 0
Player48 0 0 1 0
Player41 0 0 0 1
Player42 1 0 0 0
我尝试列出源,目标和权重列表以形成数据框,并使用邻接矩阵的概念来创建所需的矩阵。
import json
import pandas as pd
import numpy as np
with open('interactions_clean.json') as f:
data = json.load(f)
for i in data:
player_list.append(i['dataItem']['Name'])
first = []
second =[]
for i in range(len(players) - 1):
first.append(players[i])
second.append(players[i+1])
weight = [1]*684
matrix_ad = pd.DataFrame({'source': first, "target": second, 'weight': weight})
vals = np.unique(matrix_ad[['source', 'target']])
因此,为了创建所需的矩阵,我尝试了几件事,
x= matrix_ad.pivot_table(index='source',columns='target',values='weight')
这确实给了我一个矩阵,但未能添加重复对的出现。
答案 0 :(得分:0)
删除唯一行:
vals = np.unique(matrix_ad[['source', 'target']])
,应该很好。您不想在此删除重复项。