1

How to convert a list of dataframes to a dataframe of lists? For example:

lst = [pd.DataFrame({'A': [1, 2], 'B': [10, 20]}), pd.DataFrame({'A': [3, 4, 5], 'B': [30, 40, 50]})] 

Expected result:

 A B 0 [1, 2] [10, 20] 1 [3, 4, 5] [30, 40, 50] 

My real list contains thousands of dataframes.

2 Answers 2

2
lst = [pd.DataFrame({'A': [1, 2], 'B': [10, 20]}), pd.DataFrame({'A': [3, 4, 5], 'B': [30, 40, 50]})] dic_lst = [df.to_dict(orient='list') for df in lst] pd.DataFrame(dic_lst) 
Sign up to request clarification or add additional context in comments.

2 Comments

Don't know how efficient it's for thousands of dataframes... but it's the most straightforward solution I can think of.
Nice one. It should be quite fast ;)
2

We could do

out = pd.concat(dict(enumerate(lst))).groupby(level=0).agg(list) Out[1053]: A B 0 [1, 2] [10, 20] 1 [3, 4, 5] [30, 40, 50] 

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.