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 convert the contents from 2 cells in a row (df) to say several more cells in the same row on Python? Pandas related

Let’s say I have the following df (it’s a little sample from a bigger one), each row contains 3 cells:

  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
3 Fondo+Cuerpo+Ojos+Color+Pinzas+Puas Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+Purpura.png
4 Fondo+Cuerpo+Ojos+Color+Pinzas+Puas Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+Verde.png
.
.
.

All the cells from the column Permutations remain constant, this df represents some Cartesian Products that were obtained as a result of a particular process.

How could I manage to make it look like this one below?

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

  Fondo            Cuerpo                     Ojos             Color              Pinzas      Puas   
0 Fondo Oceano.png Cuerpo Cuerpo_cangrejo.png Ojos Antenas.png Color Amarillo.png Pinzas None Puas None
1 Fondo Oceano.png Cuerpo Cuerpo_cangrejo.png Ojos Antenas.png Color Amarillo.png Pinzas None Puas Arena.png
2 Fondo Oceano.png Cuerpo Cuerpo_cangrejo.png Ojos Antenas.png Color Amarillo.png Pinzas None Puas Marron.png
3 Fondo Oceano.png Cuerpo Cuerpo_cangrejo.png Ojos Antenas.png Color Amarillo.png Pinzas None Puas Purpura.png
4 Fondo Oceano.png Cuerpo Cuerpo_cangrejo.png Ojos Antenas.png Color Amarillo.png Pinzas None Puas Verde.png
.
.
.

>Solution :

You can extract the column names from the Permutations column by splitting it with + sign. Similarly, you can extract data from FilePermutations column.

import pandas as pd

old_df = pd.read_csv("cartesian.csv")
new_columns = old_df.iloc[0]['Permutations'].split("+")
new_data = []
for i in range(0, len(old_df)):
    row_data = old_df.iloc[i]['FilePermutations'].split("+")
    current_data = []
    for j, column in enumerate(new_columns):
        current_data.append(f"{column} {row_data[j]}")
    new_data.append(current_data)

updated_df = pd.DataFrame(data=new_data, columns=new_columns)
print(updated_df)

Output:

              Fondo                      Cuerpo  ...       Pinzas              Puas
0  Fondo Oceano.png  Cuerpo Cuerpo_cangrejo.png  ...  Pinzas None         Puas None
1  Fondo Oceano.png  Cuerpo Cuerpo_cangrejo.png  ...  Pinzas None    Puas Arena.png
2  Fondo Oceano.png  Cuerpo Cuerpo_cangrejo.png  ...  Pinzas None   Puas Marron.png
3  Fondo Oceano.png  Cuerpo Cuerpo_cangrejo.png  ...  Pinzas None  Puas Purpura.png
4  Fondo Oceano.png  Cuerpo Cuerpo_cangrejo.png  ...  Pinzas None    Puas Verde.png

[5 rows x 6 columns]

cartesian.csv

Permutations,FilePermutations
Fondo+Cuerpo+Ojos+Color+Pinzas+Puas,Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+None
Fondo+Cuerpo+Ojos+Color+Pinzas+Puas,Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+Arena.png
Fondo+Cuerpo+Ojos+Color+Pinzas+Puas,Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+Marron.png
Fondo+Cuerpo+Ojos+Color+Pinzas+Puas,Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+Purpura.png
Fondo+Cuerpo+Ojos+Color+Pinzas+Puas,Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+Verde.png
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