I’m trying to concatenate the output of a loop into dataframe. This example is totally unrealistic, but just to try to demonstrate my problem, my error and the result I need.
JavaScript
x
9
1
for a in range(1,4):
2
3
list1 = ["22", "23", "24", "25"]
4
list2 = ["a", "b", "c", "d"]
5
6
df = pd.DataFrame({'Num': list1,'Alpha': list2})
7
8
print(df)
9
My output:
Good output
JavaScript
1
14
14
1
Num Alpha
2
0 22 a
3
1 23 b
4
2 24 c
5
3 25 d
6
4 22 a
7
5 23 b
8
6 24 c
9
7 25 d
10
8 22 a
11
9 23 b
12
10 24 c
13
11 25 d
14
Advertisement
Answer
You can do
JavaScript
1
8
1
l = []
2
for a in range(1, 4):
3
4
list1 = ["22", "23", "24", "25"]
5
list2 = ["a", "b", "c", "d"]
6
l.append(pd.DataFrame({'Num': list1, 'Alpha': list2}))
7
out = pd.concat(l,ignore_index = True)
8