How to find all combinations of DataFrame rows?

Viewed 51

Im sorry if this question is similar to others asked in this forum, but I could not find a question similar enough. I have a df with 9 colums and 3 rows and I want to find all possible combinations between these rows. I have tried to use combinations from the itertools package but i can't seem to make it work. My desired output would be a list of all possible combinations. Thank you and sorry if it's similar to other questions asked.

import pandas as pd
from itertools import combinations

df1 = pd.DataFrame({"Main1": ["Outcome1", "Outcome2", "Outcome3"],
                    "Main2": ["Outcome1", "Outcome2", "Outcome3"],
                    "Main3": ["Outcome1", "Outcome2", "Outcome3"],
                    "Main4": ["Outcome1", "Outcome2", "Outcome3"],
                    "Main5": ["Outcome1", "Outcome2", "Outcome3"],
                    "Main6": ["Outcome1", "Outcome2", "Outcome3"],
                    "Main7": ["Outcome1", "Outcome2", "Outcome3"],
                    "Main8": ["Outcome1", "Outcome2", "Outcome3"],
                    "Main9": ["Outcome1", "Outcome2", "Outcome3"]})

    Main1   Main2   Main3   Main4   Main5   Main6   Main7   Main8   Main9
0   Outcome1    Outcome1    Outcome1    Outcome1    Outcome1    Outcome1    Outcome1    Outcome1    Outcome1
1   Outcome2    Outcome2    Outcome2    Outcome2    Outcome2    Outcome2    Outcome2    Outcome2    Outcome2
2   Outcome3    Outcome3    Outcome3    Outcome3    Outcome3    Outcome3    Outcome3    Outcome3    Outcome3

all_combinations = list(combinations(df1, 3))

edit: Smaller sample and desired output:

df1 = pd.DataFrame({"Main1": ["Outcome1", "Outcome2", "Outcome3"], "Main2": ["Outcome1", "Outcome2", "Outcome3"]}) 

desired output something like this :

[["Outcome1","Outcome1"], ["Outcome1","Outcome2"], ["Outcome1","Outcome3"], ["Outcome2","Outcome1"], ["Outcome2","Outcome2"], ["Outcome2","Outcome3"], ["Outcome3","Outcome1"], ["Outcome3","Outcome2"], ["Outcome3","Outcome3"]] 
3 Answers

You are looking for a cartesian product of a list with itself.

from itertools import product

options = ['Outcome1', 'Outcome2', 'Outcome3']

result = product(options, options)
print(*result, sep='\n')

Output

('Outcome1', 'Outcome1')
('Outcome1', 'Outcome2')
('Outcome1', 'Outcome3')
('Outcome2', 'Outcome1')
('Outcome2', 'Outcome2')
('Outcome2', 'Outcome3')
('Outcome3', 'Outcome1')
('Outcome3', 'Outcome2')
('Outcome3', 'Outcome3')

Use List comprehension

>>> [[i,j] for i in df1.Main1 for j in df1.Main2]
[['Outcome1', 'Outcome1'], ['Outcome1', 'Outcome2'], ['Outcome1', 'Outcome3'], [
'Outcome2', 'Outcome1'], ['Outcome2', 'Outcome2'], ['Outcome2', 'Outcome3'], ['O
utcome3', 'Outcome1'], ['Outcome3', 'Outcome2'], ['Outcome3', 'Outcome3']]

Using itertools product

For smaller Dataframe

import pandas as pd
from itertools import product

# Define dataframe
df1 = pd.DataFrame({"Main1": ["Outcome1", "Outcome2", "Outcome3"], "Main2": ["Outcome1", "Outcome2", "Outcome3"]}) 

# Take product of row values
# Once transposed, all the columns are the rows are the same
# We take the value of first row, and repeat to get the desired product
all_combinations = list(product(np.transpose(df1.values)[0], repeat=2))

# Show result
from pprint import pprint as pp
pp(all_combinations)

Output

[('Outcome1', 'Outcome1'),
 ('Outcome1', 'Outcome2'),
 ('Outcome1', 'Outcome3'),
 ('Outcome2', 'Outcome1'),
 ('Outcome2', 'Outcome2'),
 ('Outcome2', 'Outcome3'),
 ('Outcome3', 'Outcome1'),
 ('Outcome3', 'Outcome2'),
 ('Outcome3', 'Outcome3')]

For Original DataFrame

df1 = pd.DataFrame({"Main1": ["Outcome1", "Outcome2", "Outcome3"],
                    "Main2": ["Outcome1", "Outcome2", "Outcome3"],
                    "Main3": ["Outcome1", "Outcome2", "Outcome3"],
                    "Main4": ["Outcome1", "Outcome2", "Outcome3"],
                    "Main5": ["Outcome1", "Outcome2", "Outcome3"],
                    "Main6": ["Outcome1", "Outcome2", "Outcome3"],
                    "Main7": ["Outcome1", "Outcome2", "Outcome3"],
                    "Main8": ["Outcome1", "Outcome2", "Outcome3"],
                    "Main9": ["Outcome1", "Outcome2", "Outcome3"]})
all_combinations = list(product(np.transpose(df1.values)[0], repeat=3))

pp(all_combinations)

Output

[('Outcome1', 'Outcome1', 'Outcome1'),
 ('Outcome1', 'Outcome1', 'Outcome2'),
 ('Outcome1', 'Outcome1', 'Outcome3'),
 ('Outcome1', 'Outcome2', 'Outcome1'),
 ('Outcome1', 'Outcome2', 'Outcome2'),
 ('Outcome1', 'Outcome2', 'Outcome3'),
 ('Outcome1', 'Outcome3', 'Outcome1'),
 ('Outcome1', 'Outcome3', 'Outcome2'),
 ('Outcome1', 'Outcome3', 'Outcome3'),
 ('Outcome2', 'Outcome1', 'Outcome1'),
 ('Outcome2', 'Outcome1', 'Outcome2'),
 ('Outcome2', 'Outcome1', 'Outcome3'),
 ('Outcome2', 'Outcome2', 'Outcome1'),
 ('Outcome2', 'Outcome2', 'Outcome2'),
 ('Outcome2', 'Outcome2', 'Outcome3'),
 ('Outcome2', 'Outcome3', 'Outcome1'),
 ('Outcome2', 'Outcome3', 'Outcome2'),
 ('Outcome2', 'Outcome3', 'Outcome3'),
 ('Outcome3', 'Outcome1', 'Outcome1'),
 ('Outcome3', 'Outcome1', 'Outcome2'),
 ('Outcome3', 'Outcome1', 'Outcome3'),
 ('Outcome3', 'Outcome2', 'Outcome1'),
 ('Outcome3', 'Outcome2', 'Outcome2'),
 ('Outcome3', 'Outcome2', 'Outcome3'),
 ('Outcome3', 'Outcome3', 'Outcome1'),
 ('Outcome3', 'Outcome3', 'Outcome2'),
 ('Outcome3', 'Outcome3', 'Outcome3')]
​
Related