简体   繁体   English

将列表列表转换为pandas dataframe

[英]convert list of lists to pandas dataframe

How can I transform this list of lists (or arrays?) 如何转换此列表列表(或数组?)

[((1538, 6323), (0.9995334, 0.9995334)), ((7694, 7862, 8399, 9158), array([0.99999015, 0.99997352, 0.99997993, 0.99996219]))]

to this pandas dataframe 到这个熊猫数据框

ClstId  ColInt  ColFloat
1       1538    0.9995334
1       6323    0.9995334
2       7694    0.99999015
2       7862    0.99997352
2       8399    0.99997993
2       9158    0.99996219

?

Use list comprehension with flattening: 使用列表理解和拼合:

a = [((1538, 6323), (0.9995334, 0.9995334)), ((7694, 7862, 8399, 9158),
       np.array([0.99999015, 0.99997352, 0.99997993, 0.99996219]))]

L = [(i, y[0],y[1]) for i, x in enumerate(a, 1) for y in zip(x[0], x[1])]
df = pd.DataFrame(L, columns=['ClstId','ColInt','ColFloat'])
print (df)

    ClstId  ColInt  ColFloat
0        1    1538  0.999533
1        1    6323  0.999533
2        2    7694  0.999990
3        2    7862  0.999974
4        2    8399  0.999980
5        2    9158  0.999962

Using a simple Iteration. 使用简单的迭代。

Demo: 演示:

import pandas as pd
import numpy as np
l = [((1538, 6323), (0.9995334, 0.9995334)), ((7694, 7862, 8399, 9158), np.array([0.99999015, 0.99997352, 0.99997993, 0.99996219]))]
d = {"ColInt": [], "ColFloat" : [], "ClstId": []}
for i, v in enumerate(l, 1):                    #use enumerate to get ClstId
    d["ColInt"].extend(list(v[0]))
    d["ColFloat"].extend(list(v[1]))
    d["ClstId"].extend([i]*len(v[0]))

df = pd.DataFrame(d)
print(df)

Output: 输出:

   ClstId  ColFloat  ColInt
0       1  0.999533    1538
1       1  0.999533    6323
2       2  0.999990    7694
3       2  0.999974    7862
4       2  0.999980    8399
5       2  0.999962    9158

Try this, 尝试这个,

In [18]: a = sum([zip(i[0],i[1]) for i in lst],[])

In [20]: df.DataFrame(a, columns=['ColInt','ColFloat'])
Out[20]: 
   ColInt  ColFloat
0    1538  0.999533
1    6323  0.999533
2    7694  0.999990
3    7862  0.999974
4    8399  0.999980
5    9158  0.999962

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

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