In a pandas dataframe such as:
data = [['Ben', 'Apple', 'Banana'], ['Nancy', 'Pear', 'Apple'], ['Paul', 'Banana', 'Mango'], ['Sally', 'Mango', 'Pear']]
df = pd.DataFrame(data, columns = ['Name', 'Like', 'Dislike'])
df
Name Like Dislike
0 Ben Apple Banana
1 Nancy Pear Apple
2 Paul Banana Mango
3 Sally Mango Pear
How would I match the fruits in the Like and Dislike columns and create a df or list of who should trade with who?
I would like to return a df of something like:
trades = [['Ben', 'Paul'], ['Paul', 'Sally'], ['Sally', 'Nancy'], ['Nancy', 'Ben']]
tradesDF = pd.DataFrame(trades, columns = ['From', 'To'])
From To
0 Ben Paul
1 Paul Sally
2 Sally Nancy
3 Nancy Ben
The part I am struggling with the most is the match finding, I understand how to compare rows and columns, but individual values across the DF is a struggle, and seems like there needs to be some recursive method here. Thanks.
