Home > Back-end >  How can I convert the contents from 2 cells in a row (df) to say several more cells in the same row
How can I convert the contents from 2 cells in a row (df) to say several more cells in the same row

Time:01-09

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?

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

CodePudding user response:

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
  •  Tags:  
  • Related