Ajith
Ajith

Reputation: 1545

Python data Frame get all permutations in same column

I have a python data frame containing list of words.

Column Name
 1. text1  
 2. text2
 3. text3

I need to find all permutations from the data frame with 3 words at a time in a single column seperated by space. The output has to look like below.

    text1 text2 text3
    text1 text3 text2 
    text2 text3 text1
    text2 text1 text3
    text3 text2 text1
    text3 text1 text2

Any help on this is appreciated!!

Upvotes: 3

Views: 1753

Answers (1)

Engineero
Engineero

Reputation: 12928

Itertools is great for this! Specifically, itertools.permutations:

import itertools as it

df = pd.DataFrame({'col': ['text1', 'text2', 'text3']})
perms = it.permutations(df.col)

This gives you a generator, perms, that will give you the next permutation every time you call next(perms), something that things like for perm in perms: or [perm for perm in perms] does automatically.

Note that if you have more than three elements in your dataframe, but only want permutations of three at a time, you would use it.permutations(df.col, 3) in the above. Also note that you will get a TON of permutations if you have a lot of elements in your dataframe. It's the binomial "number of permutations equals n choose k", or N = n! / (k! * (n - k)!).

You can get the output format you want with something like:

result = '\n'.join([' '.join([s for s in perm]) for perm in perms])
print(result)

text1 text2 text3
text1 text3 text2
text2 text1 text3
text2 text3 text1
text3 text1 text2
text3 text2 text1

Upvotes: 4

Related Questions