2

I have a pandas DataFrame with n ids as columns and indices. Is there some easy method that I'm not considering to convert this to n2 rows of distances like so?

    id1 id2
id1   0 .75
id2 .75   0

to

id1   id2    .75
id1   id1      0
id2   id2      0
id2   id1    .75
Alex Riley
  • 169,130
  • 45
  • 262
  • 238
Cody Braun
  • 659
  • 6
  • 17

1 Answers1

1

You could use stack():

>>> df.stack()
id1  id1    0.00
     id2    0.75
id2  id1    0.75
     id2    0.00
dtype: float64

This creates a Series with a MultiIndex - the distances in your original DataFrame become the values. This generalises to any shape of DataFrame.

Alex Riley
  • 169,130
  • 45
  • 262
  • 238