[英]How can I use for loops to convert the columns and its contents from a df to a more organized ones on Python? Pandas related
[英]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
假设我有以下df
(这是一个更大的样本),每行包含 3 个单元格:
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
.
.
.
Permutations
列中的所有单元格都保持不变,这个df
表示由于特定过程而获得的一些笛卡尔积。
我怎样才能使它看起来像下面这样?
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
.
.
.
您可以通过用+
号拆分Permutations
列来提取列名。 同样,您可以从FilePermutations
列中提取数据。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.