The trick here is to work out a little work with the file frame. You can break this down into three steps:
- compile a list of all tuples
(a,b) - assign a random value to each pair so that
(a,b) and (b,a) have the same value - fill in a new column
Assuming your framework is called df , we can make a list of all pairs arranged so that a <= b . I think this will be easier than trying to track both (a,b) and (b,a) .
pairs = set([(a,b) if a <= b else (b,a) for a, b in df.itertuples(index=False,name=None))
It is simple enough to assign a random number to each of these pairs and store it in a dictionary, so I will leave it to you. Name it pair_dict .
Now we just need to find the values. Ultimately we want to write
df['Val'] = df.apply(<some function>, axis=1)
where our function looks at the corresponding value in pair_dict .
Instead of trying to squeeze it into a lambda (although we could), write it separately.
def func(row): if row['Sou'] <= row['Des']: key = (row['Sou'], row['Des']) else: key = (row['Des'], row['Sou']) return pair_dict[key]
hoyland
source share