简体   繁体   中英

List of dataframes to dataframe of lists

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.

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)

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM