0
l = {'col1': [[1,2,3], [4,5,6]]}

df = pd.DataFrame(data = l)

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

Desired output:

  col1
0   1
1   2
2   3
3   4
4   5
5   6
1
  • 1
    you can change the input to continuous list l = {'col1': sum([[1,2,3], [4,5,6]],[])} Commented Dec 31, 2019 at 4:35

2 Answers 2

3

Here is explode

df.explode('col1')
  col1
0    1
0    2
0    3
1    4
1    5
1    6
Sign up to request clarification or add additional context in comments.

2 Comments

My first time seeing .explode, nice!
Nice. Thanks @YO and BEN_W
2

You can use np.ravel to flatten the list of lists:

import numpy as np, pandas as pd 

l = {'col1': [[1,2,3], [4,5,6]]}
df = pd.DataFrame(np.ravel(*l.values()),columns=l.keys())

>>> df
   col1
0     1
1     2
2     3
3     4
4     5
5     6

3 Comments

Thanks, @anky_91. As you can see I prefer using numpy here, too, but understand how some might not want the additional import.
pandas operates on numpy so for that use case pd.np should work pd.DataFrame(pd.np.concatenate(*l.values()),columns=l.keys()) , but i see what you mean :)
Thank you very much @bernie

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.