Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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 a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading