Home > database >  Pandas split column of (unequal length) list into multiple columns in python
Pandas split column of (unequal length) list into multiple columns in python

Time:12-30

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 enter image description here

CodePudding user response:

You should provide an example of your desired output, but this might be what you're looking for.

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
  • Related