Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python pandas dataframe – Concat a looping df to a main df

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading