Let’s say I have the following df:
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
I know that I can use df.iloc[index_number]['Permutations'] to get the content from a particular cell, but how could I make a program that stores that information in an array such as it produces the following output?
Array1 = [‘Fondo’, ‘Cuerpo’, ‘Ojos, ‘Color’, ‘Pinzas’, ‘Puas’]
Array2 = [‘Fondo’, ‘Cuerpo’, ‘Ojos, ‘Color’, ‘Pinzas’, ‘Puas’]
Array3 = [‘Fondo’, ‘Cuerpo’, ‘Ojos, ‘Color’, ‘Pinzas’, ‘Puas’]
And so on…
In this case, words would be considered what’s before and after a + sign so I think a regex would fit here very well here.
>Solution :
Just use .str.split:
>>> arrays = df['Permutations'].str.split('+')
>>> arrays
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]
Name: Permutations, dtype: object
>>> arrays[0]
['Fondo', 'Cuerpo', 'Ojos', 'Color', 'Pinzas', 'Puas']
>>> arrays[0][3]
'Color'
And a loop using the above arrays variable:
for x, array in enumerate(arrays):
for y, item in enumerate(array):
print(f'{x}-{y}. {item}')
Output:
0-0. Fondo
0-1. Cuerpo
0-2. Ojos
0-3. Color
0-4. Pinzas
0-5. Puas
1-0. Fondo
1-1. Cuerpo
1-2. Ojos
1-3. Color
1-4. Pinzas
1-5. Puas
2-0. Fondo
2-1. Cuerpo
2-2. Ojos
2-3. Color
2-4. Pinzas
2-5. Puas
3-0. Fondo
3-1. Cuerpo
3-2. Ojos
3-3. Color
3-4. Pinzas
3-5. Puas
4-0. Fondo
4-1. Cuerpo
4-2. Ojos
4-3. Color
4-4. Pinzas
4-5. Puas