[英]creating an nxn matrix python
I have a pandas dataframe with 3 columns such that cols 1,2,3 are starting node, ending node, count, respectively;我有一个包含 3 列的 Pandas 数据框,其中 cols 1,2,3 分别是起始节点、结束节点和计数; frame head() below:帧头()如下:
index Start End count
3 31101 31101 49
231 31101 31200 152
467 31101 31201 227
706 31101 31229 77
945 31101 31247 14
1160 31101 31248 14
1399 31101 31258 11
1597 31101 31288 9
1782 31101 31613 2
1981 31101 31623 8
Start and end have the same length namely |Start|开始和结束具有相同的长度,即 |Start| = |End| = |结束| = N scalar. = N 标量。
I'd like to create an NxN matrix (in this case will be N+1 if you include the values) where the rows are the Start values and the columns are the End values and each (i,j) point value in the matrix corresponds to the count value.我想创建一个 NxN 矩阵(在这种情况下,如果包含值,则为 N+1),其中行是起始值,列是结束值和矩阵中的每个 (i,j) 点值对应于计数值。 The pair (Start(i) and End(i)) are unique.对(Start(i) 和 End(i))是唯一的。
I'm not sure how to do this and even what object to use in python.我不确定如何做到这一点,甚至不确定在 python 中使用什么对象。
It would help if you'd show the desired output, but maybe what you want is to just pivot the data?如果您显示所需的输出会有所帮助,但也许您想要的只是旋转数据?
df.pivot(index='Start', columns='End', values='count')
End 31101 31200 31201 31229 31247 31248 31258 31288 31613 31623
Start
31101 49 152 227 77 14 14 11 9 2 8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.