Issue
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.
Solution
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)
Answered By - Ignatius Reilly
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.