首页 > 解决方案 > 将字符串拆分为熊猫中的列

问题描述

我有一个例子如下:

Name        ID           Score
A           1            Math: 80.0 Physical Education: 30.5 Biology: 50.0
B           2            Math: 70.0 Physical Education: 60.5 Biology: 50.0 English 
                         Literature: 100.0
C           3            Math: 30.0 Physical Education: 10.5 Biology: 50.0

如何将“分数”列转换为列和整个数据框,如:

 Name       Subject              Score

 A          Math                 80.0
 A          Physical Education   30.5
 A          Biology              50.0

标签: stringsplit

解决方案


尝试:

x = df[["Name", "ID"]].merge(
    df["Score"]
    .str.extractall(r"(?P<Subject>[^:]+)\s*:\s*(?P<Score>[\d.]+)")
    .droplevel(1),
    left_index=True,
    right_index=True,
)
print(x)

印刷:

  Name  ID              Subject  Score
0    A   1                 Math   80.0
0    A   1   Physical Education   30.5
0    A   1              Biology   50.0
1    B   2                 Math   70.0
1    B   2   Physical Education   60.5
1    B   2              Biology   50.0
1    B   2   English Literature  100.0
2    C   3                 Math   30.0
2    C   3   Physical Education   10.5
2    C   3              Biology   50.0

推荐阅读