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.
for a in range(1,4): list1 = ["22", "23", "24", "25"] list2 = ["a", "b", "c", "d"] df = pd.DataFrame({'Num': list1,'Alpha': list2}) print(df)
My output:
Good output
Num Alpha 0 22 a 1 23 b 2 24 c 3 25 d 4 22 a 5 23 b 6 24 c 7 25 d 8 22 a 9 23 b 10 24 c 11 25 d
Advertisement
Answer
You can do
l = [] for a in range(1, 4): list1 = ["22", "23", "24", "25"] list2 = ["a", "b", "c", "d"] l.append(pd.DataFrame({'Num': list1, 'Alpha': list2})) out = pd.concat(l,ignore_index = True)