Python Forum

Full Version: How can I save cartesian products to a dataframe?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:

Output:
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.
(Dec-27-2021, 08:11 AM)noahverner1995 Wrote: [ -> ]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:

Output:
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.

Got it:

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)
Output:
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]