简体   繁体   English

如何从列表列表转换为Pandas DataFrame

[英]How do I to convert from a list of list of lists to pandas DataFrame

I have the following list of list of list and I needs to convert it into pandas DataFrame 我有以下列表列表,我需要将其转换为pandas DataFrame


[[array([['334', 0.8834404349327087, '1864004'],
         ['1613', 0.8664539456367493, '1864004'],
         ['1392', 0.6390842199325562, '1864004'],
         ['910', 0.6275900602340698, '1864004'],
         ['845', 0.6160776019096375, '1864004'],
         ['914', 0.4874180853366852, '1864004'],
         ['1109', 0.47274529933929443, '1864004'],
         ['215', 0.446095734834671, '1864004'],
         ['111', 0.4411593973636627, '1864004'],
         ['1303', 0.4356139302253723, '1864004']], dtype=object),
  array([['775', 1.040317416191101, '141243092'],
         ['1574', 0.9109242558479309, '141243092'],
         ['737', 0.8833655118942261, '141243092'],
         ['865', 0.7685815095901489, '141243092'],
         ['570', 0.7199063301086426, '141243092'],
         ['1704', 0.6063596606254578, '141243092'],
         ['604', 0.5963246822357178, '141243092'],
         ['389', 0.5376626253128052, '141243092'],
         ['815', 0.5313103795051575, '141243092'],
         ['543', 0.5247678756713867, '141243092']], dtype=object),
  array([['981', 0.8533183336257935, '1730822'],
         ['753', 0.7896251678466797, '1730822'],
         ['655', 0.664103090763092, '1730822'],
         ['345', 0.6045356392860413, '1730822'],
         ['588', 0.5033352375030518, '1730822'],
         ['496', 0.5005931258201599, '1730822'],
         ['809', 0.49608808755874634, '1730822'],
         ['884', 0.4614624083042145, '1730822'],
         ['876', 0.4543014466762543, '1730822'],
         ['1613', 0.44227200746536255, '1730822']], dtype=object)]]

With these values concatenated in the vertical. 这些值在垂直方向上串联在一起。

I tried with the usually pd.DataFrame() but is not resulting since is a list of list of lists. 我尝试使用通常的pd.DataFrame()但由于是列表列表,所以没有得到结果。

Regards 问候

So I think what you want to do would look like the following: 因此,我认为您要执行的操作如下所示:

from numpy import array
import pandas as pd

raw_data = [[array([['334', 0.8834404349327087, '1864004'],
         ['1613', 0.8664539456367493, '1864004'],
         ['1392', 0.6390842199325562, '1864004'],
         ['910', 0.6275900602340698, '1864004'],
         ['845', 0.6160776019096375, '1864004'],
         ['914', 0.4874180853366852, '1864004'],
         ['1109', 0.47274529933929443, '1864004'],
         ['215', 0.446095734834671, '1864004'],
         ['111', 0.4411593973636627, '1864004'],
         ['1303', 0.4356139302253723, '1864004']], dtype=object),
  array([['775', 1.040317416191101, '141243092'],
         ['1574', 0.9109242558479309, '141243092'],
         ['737', 0.8833655118942261, '141243092'],
         ['865', 0.7685815095901489, '141243092'],
         ['570', 0.7199063301086426, '141243092'],
         ['1704', 0.6063596606254578, '141243092'],
         ['604', 0.5963246822357178, '141243092'],
         ['389', 0.5376626253128052, '141243092'],
         ['815', 0.5313103795051575, '141243092'],
         ['543', 0.5247678756713867, '141243092']], dtype=object),
  array([['981', 0.8533183336257935, '1730822'],
         ['753', 0.7896251678466797, '1730822'],
         ['655', 0.664103090763092, '1730822'],
         ['345', 0.6045356392860413, '1730822'],
         ['588', 0.5033352375030518, '1730822'],
         ['496', 0.5005931258201599, '1730822'],
         ['809', 0.49608808755874634, '1730822'],
         ['884', 0.4614624083042145, '1730822'],
         ['876', 0.4543014466762543, '1730822'],
         ['1613', 0.44227200746536255, '1730822']], dtype=object)]]

data = raw_data[0]  # raw data is a list with one element for some reason
pd.concat(pd.DataFrame(datum) for datum in data)

Use np.vstack to get them all together from first element of your list. 使用np.vstack将它们从列表的第一个元素放在一起。 Then just simply convert it to pandas: 然后只需将其转换为熊猫:

df = pd.DataFrame(data=np.vstack(list[0]), columns=['col1', 'col2', 'col3'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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