I’m trying to concatenate a dataframe created in a loop to a main df. It seems that using .copy() or not doesn’t change the fact that every time the loop start again, the main df reset to the looping df. I’ve based my script on Appending pandas dataframes generated in a for loop but it doesn’t work.
Here’s my script:
firstloop = True
for x in x_list:
loop_db = myFunctionReturnADataframe(x)
if firstloop:
main_db = loop_db.copy()
firstloop == False
else:
main_db = pd.concat([main_db.copy(), loop_db.copy()])
Each loop the main_db doesn’t add loop_db, instead it change and equal loop_db.
What am I doing wrong?
Thanks for your time
>Solution :
In your loop, you have a tiny typo.
firstloop == False is a comparison, which returns a boolean value (False in this case). It is not setting firstloop value to False.
You need to change it to:
firstloop = False