How can I save cartesian products to a dataframe via loop on Python?

Viewed 47

I have the following dictionary:

the_dictionary_list = {'Fondo': ['Oceano.png'],
                                'Cuerpo': ['Cuerpo_cangrejo.png'], 
                                'Ojos': ['Antenas.png', 'Pico.png', 'Verticales.png'],
                                'Color': ['Amarillo.png', 'Blanco.png', 'Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'], 
                                'Pinzas': ['None', 'Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'], 
                                'Puas': ['None', 'Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}

And to get each possible permutation without repetition in a specific order (i.e. cartesian products) I use the following code:

import itertools as it


AllKeysNames = ['Fondo', 'Cuerpo', 'Ojos', 'Color', 'Pinzas', 'Puas']
Combinations = list(it.product(*(the_dictionary_list[Name] for Name in AllKeysNames)))
print(f'{Combinations}')

How can I make the above program saves each iteration to a dataframe such as it throws an output like this one:

Index   |                    Permutations   |        FilePermutations
0       |Fondo+Cuerpo+Ojos+Color+Pinzas+Puas|Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+None
1       |Fondo+Cuerpo+Ojos+Color+Pinzas+Puas|Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+Arena.png
2       |Fondo+Cuerpo+Ojos+Color+Pinzas+Puas|Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+Marron.png
.
.
.

The information within the Permutations column will remain constant, and the dataframe would ended up with an index length of 359

2 Answers

IIUC, one way using pandas.Series.str.cat:

df = pd.DataFrame(list(product(*the_dictionary_list.values())))
df = df.loc[:, 0].str.cat(df.loc[:, 1:], sep="+").to_frame(name="FilePermutations")

df["Permutations"] = "+".join(the_dictionary_list)
print(df)

Output:

                                      FilePermutations  \
0    Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Ama...   
1    Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Ama...   
2    Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Ama...   
3    Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Ama...   
4    Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Ama...   
..                                                 ...   
355  Oceano.png+Cuerpo_cangrejo.png+Verticales.png+...   
356  Oceano.png+Cuerpo_cangrejo.png+Verticales.png+...   
357  Oceano.png+Cuerpo_cangrejo.png+Verticales.png+...   
358  Oceano.png+Cuerpo_cangrejo.png+Verticales.png+...   
359  Oceano.png+Cuerpo_cangrejo.png+Verticales.png+...   

                            Permutations  
0    Fondo+Cuerpo+Ojos+Color+Pinzas+Puas  
1    Fondo+Cuerpo+Ojos+Color+Pinzas+Puas  
2    Fondo+Cuerpo+Ojos+Color+Pinzas+Puas  
3    Fondo+Cuerpo+Ojos+Color+Pinzas+Puas  
4    Fondo+Cuerpo+Ojos+Color+Pinzas+Puas  
..                                   ...  
355  Fondo+Cuerpo+Ojos+Color+Pinzas+Puas  
356  Fondo+Cuerpo+Ojos+Color+Pinzas+Puas  
357  Fondo+Cuerpo+Ojos+Color+Pinzas+Puas  
358  Fondo+Cuerpo+Ojos+Color+Pinzas+Puas  
359  Fondo+Cuerpo+Ojos+Color+Pinzas+Puas  

[360 rows x 2 columns]

Add join for it.product and then join for Permutations column:

AllKeysNames = ['Fondo', 'Cuerpo', 'Ojos', 'Color', 'Pinzas', 'Puas']

new = ['+'.join(x) for x in 
                        it.product(*(the_dictionary_list[Name] for Name in AllKeysNames))]

df = pd.DataFrame({'Permutations':"+".join(AllKeysNames), 'FilePermutations':new})

print (df)
                            Permutations  \
0    Fondo+Cuerpo+Ojos+Color+Pinzas+Puas   
1    Fondo+Cuerpo+Ojos+Color+Pinzas+Puas   
2    Fondo+Cuerpo+Ojos+Color+Pinzas+Puas   
3    Fondo+Cuerpo+Ojos+Color+Pinzas+Puas   
4    Fondo+Cuerpo+Ojos+Color+Pinzas+Puas   
..                                   ...   
355  Fondo+Cuerpo+Ojos+Color+Pinzas+Puas   
356  Fondo+Cuerpo+Ojos+Color+Pinzas+Puas   
357  Fondo+Cuerpo+Ojos+Color+Pinzas+Puas   
358  Fondo+Cuerpo+Ojos+Color+Pinzas+Puas   
359  Fondo+Cuerpo+Ojos+Color+Pinzas+Puas   

                                      FilePermutations  
0    Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Ama...  
1    Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Ama...  
2    Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Ama...  
3    Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Ama...  
4    Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Ama...  
..                                                 ...  
355  Oceano.png+Cuerpo_cangrejo.png+Verticales.png+...  
356  Oceano.png+Cuerpo_cangrejo.png+Verticales.png+...  
357  Oceano.png+Cuerpo_cangrejo.png+Verticales.png+...  
358  Oceano.png+Cuerpo_cangrejo.png+Verticales.png+...  
359  Oceano.png+Cuerpo_cangrejo.png+Verticales.png+...  

[360 rows x 2 columns]
Related