Create a data frame containin all possible permutations/combination given a string specifying the number of repetitions

Question:

I would like to create a data frame that contains by row the 24^ combinations for all the letters of the alphabet, i.e. starting with:

begin = pd.DataFrame({
"combi":["AAA","AAB","AAC","AAD"]})

and ending with:

end = pd.DataFrame({
"combi":["ZZW","ZZX","ZZY","ZZZ"]})

this is what if found out:

names = list(itertools.combinations(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'], 3))
#names['concat'] = pd.Series(names.fillna('').values.tolist()).str.join('')
print(names)

Contentwise it is fine but I need it in the shape as above.

Asked By: highbury

||

Answers:

Here is a possible approach using product() from itertools:

import pandas as pd
from itertools import product

combi = list(product(list('ABCDEFGHIJKLMNOPQRSTUVWXYZ'), repeat=3))
df = pd.DataFrame({'combi': [''.join(alpha) for alpha in combi]})
print(df)

You could also get the alphabets using string.ascii_uppercase by importing string instead of having to write them.

import pandas as pd
from itertools import product
import string

alphabets = string.ascii_uppercase
alpha_combi = [''.join(alpha) for alpha in product(alphabets, repeat=3)]
df = pd.DataFrame({'combi': alpha_combi})
print(df)


      combi
0       AAA
1       AAB
2       AAC
3       AAD
4       AAE
...     ...
17571   ZZV
17572   ZZW
17573   ZZX
17574   ZZY
17575   ZZZ
Answered By: Jamiu S.