I have this DF:
Elemento_lista | DISPLAYNAME | Rdo_Encontrado |
---|---|---|
0 | 90S VALASION | [('90S VALASION', 100, 0), ('90S VALASION', 100, 1), ('90S VALASION', 100, 2), ('90S VALASION', 100, 3)] |
1 | ADIZERO RC 2 W | [('ADIZERO RC 2 W', 100, 11), ('ADIZERO RC 2 W', 100, 12), ('ADIZERO RC 2 W', 100, 13), ('ADIZERO RC 2 W', 100, 14)] |
2 | ASWEERUN | [('ASWEERUN', 100, 16), ('ASWEERUN', 100, 17), ('90S VALASION', 34, 0), ('90S VALASION', 34, 1)] |
As it can bee seen, Rdo_Encontrado Column´s value is a List. I need a new row, for each element, and 3 new columns, with values from each list element. The Output should look like this:
Elemento_lista | DISPLAYNAME | Coincidencia | Score | idx |
---|---|---|---|---|
0 | 90S VALASION | 90S VALASION | 100 | 0 |
0 | 90S VALASION | 90S VALASION | 100 | 1 |
0 | 90S VALASION | 90S VALASION | 100 | 2 |
0 | 90S VALASION | 90S VALASION | 100 | 3 |
1 | ADIZERO RC 2 W | ADIZERO RC 2 W | 100 | 11 |
1 | ADIZERO RC 2 W | ADIZERO RC 2 W | 100 | 12 |
1 | ADIZERO RC 2 W | ADIZERO RC 2 W | 100 | 13 |
1 | ADIZERO RC 2 W | ADIZERO RC 2 W | 100 | 14 |
2 | ASWEERUN | ASWEERUN | 100 | 16 |
2 | ASWEERUN | ASWEERUN | 100 | 17 |
2 | ASWEERUN | ASWEERUN | 34 | 0 |
2 | ASWEERUN | ASWEERUN | 34 | 1 |
Is it possible to do something like this?. I couldn´t find a way...
CodePudding user response:
Try:
# make sure you don't have strings in Rdo_Encontrado column:
from ast import literal_eval
df["Rdo_Encontrado"] = df["Rdo_Encontrado"].apply(literal_eval)
df = df.explode("Rdo_Encontrado")
df[["Coincidencia", "Score", "idx"]] = df.pop("Rdo_Encontrado").apply(pd.Series)
print(df.to_markdown())
Prints:
Elemento_lista | DISPLAYNAME | Coincidencia | Score | idx | |
---|---|---|---|---|---|
0 | 0 | 90S VALASION | 90S VALASION | 100 | 0 |
0 | 0 | 90S VALASION | 90S VALASION | 100 | 1 |
0 | 0 | 90S VALASION | 90S VALASION | 100 | 2 |
0 | 0 | 90S VALASION | 90S VALASION | 100 | 3 |
1 | 1 | ADIZERO RC 2 W | ADIZERO RC 2 W | 100 | 11 |
1 | 1 | ADIZERO RC 2 W | ADIZERO RC 2 W | 100 | 12 |
1 | 1 | ADIZERO RC 2 W | ADIZERO RC 2 W | 100 | 13 |
1 | 1 | ADIZERO RC 2 W | ADIZERO RC 2 W | 100 | 14 |
2 | 2 | ASWEERUN | ASWEERUN | 100 | 16 |
2 | 2 | ASWEERUN | ASWEERUN | 100 | 17 |
2 | 2 | ASWEERUN | 90S VALASION | 34 | 0 |
2 | 2 | ASWEERUN | 90S VALASION | 34 | 1 |
EDIT: Added ast.literal_eval