pandas df 元素从列表到列
pandas df elements from list into column
我有这个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)]
可以看出,Rdo_Encontrado 列的值是一个列表。我需要一个新行,每个元素,和 3 个新列,每个列表元素的值。输出应如下所示:
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
有没有可能做这样的事情?我找不到办法...
尝试:
# 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())
打印:
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
编辑:添加了 ast.literal_eval
我有这个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)] |
可以看出,Rdo_Encontrado 列的值是一个列表。我需要一个新行,每个元素,和 3 个新列,每个列表元素的值。输出应如下所示:
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 |
有没有可能做这样的事情?我找不到办法...
尝试:
# 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())
打印:
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 |
编辑:添加了 ast.literal_eval