I get data in this format..
ListA =
[
[('test1', 'aaa', 'A'),('test2', 'bbb', 'B'),('test3', 'ccc', 'C')],
[('test4', 'ddd', 'D'),('test5', 'eee', 'E'),('test6', 'fff', 'F')],
[('test7', 'ggg', 'A'),('test8', 'hhh', 'B'),('test9', 'ppp', 'C')]
]
and I would like to transform to this format
ID, ColA, ColB, ColC,
1, 'test1', 'aaa', 'A'
1, 'test2', 'bbb', 'B'
1, 'test3', 'ccc', 'C'
2, 'test4', 'ddd', 'D'
2, 'test5', 'eee', 'E'
2, 'test6', 'fff', 'F'
3, 'test7', 'ggg', 'A'
3, 'test8', 'hhh', 'B'
3, 'test9', 'ppp', 'C'
>Solution :
You can use itertools.chain:
from itertools import chain
df = pd.DataFrame(chain.from_iterable(ListA),
columns=['ColA', 'ColB', 'ColC'])
output:
ColA ColB ColC
0 test1 aaa A
1 test2 bbb B
2 test3 ccc C
3 test4 ddd D
4 test5 eee E
5 test6 fff F
6 test7 ggg A
7 test8 hhh B
8 test9 ppp C
with the index (can handle uneven list lengths):
from itertools import chain
import numpy as np
idx = np.repeat(np.arange(len(ListA))+1, list(map(len, ListA)))
df = pd.DataFrame(chain.from_iterable(ListA),
columns=['ColA', 'ColB', 'ColC'],
index=idx).rename_axis('ID')
output:
ColA ColB ColC
ID
1 test1 aaa A
1 test2 bbb B
1 test3 ccc C
2 test4 ddd D
2 test5 eee E
2 test6 fff F
3 test7 ggg A
3 test8 hhh B
3 test9 ppp C