mathcomp guy
mathcomp guy

Reputation: 139

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; 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| = |End| = N scalar.

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. The pair (Start(i) and End(i)) are unique.

I'm not sure how to do this and even what object to use in python.

Upvotes: 1

Views: 296

Answers (1)

Chris
Chris

Reputation: 16137

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

Upvotes: 1

Related Questions