0

As the title goes this is very similar to: Split a Pandas column of lists into multiple columns

However, in my df I don't know a priori the length of the longest list and I find not efficient assigning columns manually.

For now I convert the lists to strings and then I use:

df = pd.DataFrame(d1['l'].astype(str).str.replace("[\[\],\']","",regex=True).str.split().values.tolist())

input:

d1 = {'teams': [['SF', 'NYG', 'aks', 'tls', 'slp', 'raa', 'gla'],['SF', 'NYG'],['SF', 'NYG'],
                ['SF', 'NYG'],['SF', 'NYG'],['SF', 'NYG'],['SF', 'NYG']]}
d1 = pd.DataFrame(d1)
print (df2)
                                teams
0  [SF, NYG, aks, tls, slp, raa, gla]
1                           [SF, NYG]
2                           [SF, NYG]
3                           [SF, NYG]
4                           [SF, NYG]
5                           [SF, NYG]
6                           [SF, NYG]


Out: 
                            team1         team2           teamn
0                           SF          NYG     ....      gla
1                           SF          NYG
2                           SF          NYG               None
3                           SF          NYG               None
4                           SF          NYG
5                           SF          NYG
6                           SF          NYG

Any quicker alternative?

KArrow'sBest
  • 150
  • 9

0 Answers0