-4

My dataframe looks like this:

Source  Target  Value  Source location  Target location
a       b       10     [12.3,1.9]       [13.5,14.3]
c       v       10     [15.3,1.9]       [13.5,94.3]
c       d       18     [1.4,31.9]       [16.6,44.7]
p       q       10     [12.3,1.9]       [13.5,15.3]
x       z        8     [6.3,1.4]        [47.5,4.3]

I want to find both the pairs between Source and Target. I'm expecting my end result to look like this:

Source  Target  Value  Source location  Target location
a       b       10     [12.3,1.9]       [13.5,14.3]
b       a        9     [72.9,18.6]       [14.2,31.6]
c       e       18     [1.4,31.9]       [16.6,44.7]
e       c       14     [19.4,5.1]       [12.3,23.4]
z       x        6     [92.3,1.9]       [43.5,14.3]
x       z        8     [6.3,1.4]        [47.5,4.3]

As you can see, I want to get both the pairs of Source and Target - i.e. a-c as well as c-a as the two rows.

I tried to research on Stackoverflow but it seems like this question hasn't been answered before. I looked at this and this but they use groupby to find the count of columns not rearrange the existing dataframe. This one was the closest I got but it returns dataframes on unique combinations of rows not columns. Any help would be appreciated.

Abhishek
  • 553
  • 2
  • 9
  • 26

1 Answers1

0

One of possible solutions is:

Define a function to be applied soon:

def fn(row):
    r2 = row.copy()
    r2.Source, r2.Target = r2.Target, r2.Source
    r2['Source location'], r2['Target location'] =\
        r2['Target location'], r2['Source location']
    return r2

Then run:

pd.concat([df, df.apply(fn, axis=1)]).sort_index()

This instruction:

  • Applies fn function to df, yielding a "shaddow" DataFrame with "pair-reversed" columns
    • Source and Target,
    • Source location and Target location,
  • Concatenates them.
  • Sorts by index.

If you are unhappy about "doubled" index values, add .reset_index(drop=True).

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41