1

All possible combinations of three columns

I am not able to do it with itertools.combinations or itertools.permutations.

Input dataframe :

a   b   c
1   101 1001
2   102 1002
3   103 1003

Expected dataframe :

a   b   c
1   101 1001
1   101 1002
1   101 1003
1   102 1001
1   102 1002
1   102 1003
1   103 1001
1   103 1002
1   103 1003
2   101 1001
2   101 1002
2   101 1003
2   102 1001
2   102 1002
2   102 1003
2   103 1001
2   103 1002
2   103 1003
3   101 1001
3   101 1002
3   101 1003
3   102 1001
3   102 1002
3   102 1003
3   103 1001
3   103 1002
3   103 1003
0

1 Answer 1

4

Use itertools.product:

from  itertools import product
#all columns
df = pd.DataFrame(list(product(*df.values.T)))
#if you need to specify columns
#df = pd.DataFrame(list(product(*[df.a, df.b, df.c])))
print (df)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.