0

I have a dataframe with three columns and I want to find the quickest way to transform it to a distance matrix.

the dataframe is like :

A    |   B  |   distance_A_B
-----------------------------
a1   |  a2  |    0.3
a1   |  a3  |    0.5
a2   |  a1  |    0.3

And I want to have a distance matrix like this with python:

M[i][j] = distance(ai, aj)

I already have distances between a_i and a_j ! it's just about transforming the dataframe to matrix, I did it with two loops but I need to optimize my code

The expected answer for this example would be :

np.array([[0,0.3,0.5],[0.3,0,0],[0.5,0,0])

Thank you.

Hanxue
  • 12,243
  • 18
  • 88
  • 130
Oussama Jabri
  • 674
  • 1
  • 7
  • 18

0 Answers0