1

I am trying to convert a list within multiple columns of a pandas DataFrame into separate columns.

Say, I have a dataframe like this:

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

And would like to convert it to something like this:

   0  1  2  0  1  2
0  1  2  3  4  5  6
1  1  2  3  4  5  6
2  1  2  3  4  5  6

I have managed to do this in a loop. However, I would like to do this in fewer lines. My code snippet so far is as follows:

import pandas as pd

df = pd.DataFrame([[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]])
output1 = df[0].apply(pd.Series)
output2 = df[1].apply(pd.Series)

output = pd.concat([output1, output2], axis=1)
2
  • Are you sure you want that output? Duplicate column names are allowed but not particularly useful... Commented Nov 22, 2018 at 15:13
  • At this point I am not really worried about the duplicated column names. I would like to reduce it to one line instead of implementing what will be loop/concat structure once applied to a dataframe with more than two columns Commented Nov 22, 2018 at 15:18

2 Answers 2

2

If you don't care about the column names you could do:

>>> df.apply(np.hstack, axis=1).apply(pd.Series)
   0  1  2  3  4  5
0  1  2  3  4  5  6
1  1  2  3  4  5  6
2  1  2  3  4  5  6
Sign up to request clarification or add additional context in comments.

Comments

1

Using sum

pd.DataFrame(df.sum(1).tolist())
   0  1  2  3  4  5
0  1  2  3  4  5  6
1  1  2  3  4  5  6
2  1  2  3  4  5  6

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.