urschrei
urschrei

Reputation: 26859

Pandas: compute Series or DataFrame permutations

I have a dataframe of rows, indexed by name, and a single column containing a point tuple:

import pandas as pd
d = {'coords': {'a': (1, 2), 'b': (3, 4), 'c': (5, 6), 'd': (7, 8)}}
df = pd.dataframe(d)

What I'd like to do is retrieve data in the same way as if I ran itertools.permutations on the point data tuples, with a tuple-length of 2:

from itertools import permutations
list(permutations([(1, 2), (3, 4), (5, 6), (7, 8)], 2))

[((1, 2), (3, 4)),
 ((1, 2), (5, 6)),
 ((1, 2), (7, 8)),
 ((3, 4), (1, 2)),
 ((3, 4), (5, 6)),
 ((3, 4), (7, 8)),
 ((5, 6), (1, 2)),
 ((5, 6), (3, 4)),
 ((5, 6), (7, 8)),
 ((7, 8), (1, 2)),
 ((7, 8), (3, 4)),
 ((7, 8), (5, 6))]

The aim here is easy retrieval of point coordinates for any combination of two places (a, b --> (1, 2), (3, 4) etc.), but I have no idea how to calculate this, or whether I could use a MultiIndex to do it. An index-based solution would be ideal, because I'd also like to store data (e.g. a computed route) for each location pair.

Upvotes: 0

Views: 760

Answers (1)

Woody Pride
Woody Pride

Reputation: 13965

Using your df as the starting point:

Index = list(permutations(df.index, 2))
new_df = pd.DataFrame({
         'route' : [[df.loc[Ind[0], 'coords'], df.loc[Ind[1], 'coords']] for Ind in Index]
                      }, index = Index)

Not sure if this is what you want, but this gives me this:

In [21]: new_df
Out[21]:
                   route
(a, b)  [(1, 2), (3, 4)]
(a, c)  [(1, 2), (5, 6)]
(a, d)  [(1, 2), (7, 8)]
(b, a)  [(3, 4), (1, 2)]
(b, c)  [(3, 4), (5, 6)]
(b, d)  [(3, 4), (7, 8)]
(c, a)  [(5, 6), (1, 2)]
(c, b)  [(5, 6), (3, 4)]
(c, d)  [(5, 6), (7, 8)]
(d, a)  [(7, 8), (1, 2)]
(d, b)  [(7, 8), (3, 4)]
(d, c)  [(7, 8), (5, 6)]

Upvotes: 2

Related Questions