1

I am trying to break a column of list value into different column. Can you guys please give me some guidance how to do that? Thanks In the original dataframe, there is no column name(Column 1 Column2). I just name it in order to have better understanding on this issue.

Before:
Column 1   Column2
   A       ['2.49', '-2.18', '-3.79']
   B       ['2.56', '-3.02', '-4.92']
   C       ['-0.09', '-1.73', '-3.47']

After:
Column 1   Column2      Column3       Column4
   A       '2.49'       '-2.18'       '-3.79'
   B       '2.56'       '-3.02'       '-4.92'
   C       '-0.09'      '-1.73'       '-3.47'
  • 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) – DYZ Jun 23 '20 at 06:06
  • `pd.concat([df, pd.DataFrame(df.column2.tolist())],axis = 1)` – Onyambu Jun 23 '20 at 06:14

2 Answers2

1
df = pd.DataFrame({'Column 1': ['A', 'B', 'C'],
                   'Column2': [['2.49', '-2.18', '-3.79'], ['2.56', '-3.02', '-4.92'], ['-0.09', '-1.73', '-3.47']]
                  }
                 )

pd.concat([df['Column 1'],
           pd.DataFrame(df['Column2'].to_list(), columns=['Column2', 'Column3', 'Column4'])],
          axis=1)
René
  • 4,594
  • 5
  • 23
  • 52
-1

Since the values of Column 2 are list you don't actually need splits or anything like that, just select the element that you want. So, column3 will be list element[1], column4 list element[2], and then lastly convert column2 to list element[0]:

df['Column 3'] = df['Column 2'][1]
df['Column 4'] = df['Column 2'][2]
df['Column 2'] = df['Column 2'][0]

I would recommend to rename them so you don't overwrite the 'Column2' and keep all the columns.

Martijniatus
  • 102
  • 3