简体   繁体   English

Pandas 将(不等长度)列表的列拆分为 python 中的多个列

[英]Pandas split column of (unequal length) list into multiple columns in python

I have a list of codes in one column his name (Course code) that I would like to put each code is a new column我在一列中有一个代码列表他的名字(课程代码)我想把每个代码都是一个新列

Semester number            Course code                          Semester AVREGE
0   13          ['ZY705']                                       S         GOOD
1   14          ['ZY405', 'ZY504', 'ZY510', 'ZY601', 'ZY605']   S         FAIL
2   15          ['ZY504', 'ZY601', 'ZY603']                     F         FAIL
3   16          ['ZY504', 'ZY704', 'ZY705']                     S         FAIL
4   17          ['ZY704']                                       F         FAIL

Can you guys please give me some guidance how to do that?各位大神能给我一些指导如何做到这一点吗? Thanks谢谢在此处输入图像描述

IIUC:国际大学联盟:

df = df.join(df.pop('Course code').explode().reset_index().assign(dummy=1) \
               .pivot_table('dummy', 'index', 'Course code', fill_value=0))
print(df)

# Output
   Semester number Semester AVREGE  ZY405  ZY504  ZY510  ZY601  ZY603  ZY605  ZY704  ZY705
0               13        S   GOOD      0      0      0      0      0      0      0      1
1               14        S   FAIL      1      1      1      1      0      1      0      0
2               15        F   FAIL      0      1      0      1      1      0      0      0
3               16        S   FAIL      0      1      0      0      0      0      1      1
4               17        F   FAIL      0      0      0      0      0      0      1      0

You should provide an example of your desired output, but this might be what you're looking for.您应该提供您想要的 output 的示例,但这可能是您正在寻找的。

pd.concat([df, pd.DataFrame(df['Course code'].to_list())], axis=1).drop(columns='Course code')

   Semester number Semester AVREGE        0        1        2        3        4
0               13        S   GOOD  'ZY705'     None     None     None     None
1               14        S   FAIL  'ZY405'  'ZY504'  'ZY510'  'ZY601'  'ZY605'
2               15        F   FAIL  'ZY504'  'ZY601'  'ZY603'     None     None
3               16        S   FAIL  'ZY504'  'ZY704'  'ZY705'     None     None
4               17        F   FAIL  'ZY704'     None     None     None     None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM