Right, So I'm a Python rookie/beginner but tried a few different things with bits and pieces from what I could find:
import pandas as pd
import numpy as np
df = pd.DataFrame({'node1':['A','A','B','B','C','C'],
'node2':['B','C','A','C','A','B'],
'value':[0.05,0.84,0.05,0.97,0.84,0.97]})
df = df[['value','node1','node2']] #Step 1: re-arange dataframe to work properly
df['value'] = df['value'].astype(str) #Step 2: float type (numbers) to string type
df = pd.DataFrame(np.sort(df.values, axis=1), columns=df.columns).drop_duplicates() #Step 3: Sort and drop dups
df['value'] = df['value'].astype(float) #Step 4: string type back to float
df = df[['node1','node2','value']] #Step 5: re-arange dataframe back to starting positions
print(df)
I used and want to refer to:
- This Pandas documentation on Panda's
.to_string
function
- This SO post to re-position columns as I found out
np.sort
didn't shift headers.
- This SO post to sort values and drop duplicates.
I'm a 100% sure that this can be shortened to a smoother piece of code by the right person. Hope it helps in the meantime!