Reputation: 26
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
?
Upvotes: 0
Views: 3925
Reputation: 863501
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
Upvotes: 1
Reputation: 82785
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
Upvotes: 0
Reputation: 16081
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
Upvotes: 0