0

I have these values in dataset in a pandas dataframe column

col1
[[1,2],[1,2]]
[[3,4],[3,4]]
[[5,6],[5,6]]

I want to get a new column of two elements as list in new columns as rows.

This is the columns that I want to get.

col1    col2
[1,1]   [2,2] 
[3,3]   [4,4]
[5,5]   [6,6]

2 Answers2

1

Assuming lists, use the DataFrame constructor:

out = pd.DataFrame(df['col1'].tolist(), columns=['col1', 'col2'])

If you have strings, first convert to lists:

df['col1'] = df['col1'].apply(pd.eval)
mozway
  • 194,879
  • 13
  • 39
  • 75
1

Assuming so is the name of your dataframe and "a" the name of the original column you want to split you can do it using apply-lambda approach. I am not sure if that is the best way:

so["b"] = so["a"].apply(lambda x: x[1])
so["a"] = so["a"].apply(lambda x: x[0])
Borja_042
  • 1,071
  • 1
  • 14
  • 26