小编M. *_*ger的帖子

如何将两列数组转换为具有出现次数的矩阵?

我有以下 numpy 数组:

import numpy as np

pair_array = np.array([(205, 254), (205, 382), (254, 382), (18, 69), (205, 382), 
                       (31, 183), (31, 267), (31, 382), (183, 267), (183, 382)])

print(pair_array)

#[[205 254]
# [205 382]
# [254 382]
# [ 18  69]
# [205 382]
# [ 31 183]
# [ 31 267]
# [ 31 382]
# [183 267]
# [183 382]]
Run Code Online (Sandbox Code Playgroud)

有没有办法将此数组转换为包含所有可能组合的出现次数的对称熊猫数据框?我期待这样的事情:

#     18  31  69 183 205 254 267 382 
#  18  0   0   1   0   0 …
Run Code Online (Sandbox Code Playgroud)

python numpy networkx adjacency-matrix pandas

30
推荐指数
2
解决办法
994
查看次数

标签 统计

adjacency-matrix ×1

networkx ×1

numpy ×1

pandas ×1

python ×1