Python Forum
How can I save cartesian products to a dataframe?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I save cartesian products to a dataframe?
#1
Question 
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.
Reply
#2
(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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,136 Jan-06-2023, 10:53 PM
Last Post: haihal
  Filter value from DataFrame apply a function and save to xlsx zinho 1 2,044 Dec-22-2019, 03:54 PM
Last Post: zinho
  Diagonal grid products PythonLamer 1 2,525 Dec-05-2017, 11:32 AM
Last Post: Larz60+
  Image conversion form cartesian to polar and back to cartesian Ashwinraj 2 16,352 Aug-10-2017, 09:01 AM
Last Post: Ashwinraj

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020