0

I have a dataframe in the below format. I want to split the values of points column into different columns like A,B,C and so on based on the number of items in the list by deleting the original column.

df:
         x           y              points
    0   82.123610   16.724781   [1075038212.0, -18.099967840282456, -18.158378...
    1   82.126540   16.490998   [1071765909.0, -20.406018294234215, -15.850444...
    2   82.369578   17.402203   [1072646747.0, -16.839004016179505, -18.334996...
    3   81.612240   17.464167   [1096294130.0, -15.335239025421126, -15.303402...
  • 1
    Naming from A, B, C, ... and so on and what name should 27th column get? – Chris Sep 03 '20 at 10:09
  • Does this answer your question? [Pandas split column of lists into multiple columns](https://stackoverflow.com/questions/35491274/pandas-split-column-of-lists-into-multiple-columns) – Stas Buzuluk Sep 03 '20 at 10:35
  • @StasBuzuluk No, It is creating new columns keeping the original column. I'm looking for a simplified solution by dropping the column and creating new columns at one stretch – hashalluring Sep 03 '20 at 11:12

1 Answers1

1

I think best here is create numeric columns names:

df = df.join(pd.DataFrame(df.pop('points').tolist(), index=df.index))

If length of list is less like 27 is possible use:

import string

d = dict(enumerate(string.ascii_lowercase))

df = df.join(pd.DataFrame(df.pop('points').tolist(), index=df.index).rename(columns=d))
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252