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]
0

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

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])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.