1

I have a lists of lists, that I want to convert to one row in a DataFrame. Each list should come in a cell in the DataFrame. When completed, I want to add the next list of lists in the same way.

This is what I do:

import pandas as pd
lst1 = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]    
lst2 = [['j', 'k', 'l'], ['m', 'n', 'o'], ['p', 'q', 'r']]    
DataFrame = []
df = pd.DataFrame(lst1)
df = pd.DataFrame(lst2)

Resulting in:

          0     1    2
0         j     k    l
1         m     n    o
2         p     q    r

But what I would like is this:

         0                1                2
0        ['a', 'b', 'c']  ['d', 'e', 'f']  ['g', 'h', 'i']
1        ['j', 'k', 'l']  ['m', 'n', 'o']  ['p', 'q', 'r']

Is there a simpler and elegant way to do this?

1 Answer 1

1

So is this what you need ?

pd.DataFrame([lst1,lst2])
Out[500]: 
           0          1          2
0  [a, b, c]  [d, e, f]  [g, h, i]
1  [j, k, l]  [m, n, o]  [p, q, r]
Sign up to request clarification or add additional context in comments.

9 Comments

Yes, that is what I would like.
Man, you are too fast!
@Vaishali haha , just time to break , so looking around the site . :-)
Can lst1, lst2 etc, also be added in a loop? So that it becomes possible to do the same for 100k lists of lists?
@twhale, this is a different question. But my advice is combine first into a list of lists, then combine into one dataframe at the end. Appending to a list is much cheaper than appending to a dataframe.
|

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.