0

I want to fill an empty dataframe with data in csv files by using a loop. The code works fine except that each time the dataframe records only the last csv file and not all the csv files that exist in the destination folder.

Did I do something wrong?

Here is a piece of my code :

csv_files = glob.glob(path +"/*.csv")

for csv_file in csv_files:

     columns_name = [A,B,C,D]
     newDF = pd.DataFrame( columns=columns_name)
     newDF = pd.concat([newDF,df])
     newDF.fillna('unknown', inplace=True)

1 Answer 1

1

Just move newDF outside the loop:

csv_files = glob.glob(path +"/*.csv")

columns_name = [A,B,C,D]
newDF = pd.DataFrame( columns=columns_name)

for csv_file in csv_files:
     newDF = pd.concat([newDF,df])
     newDF.fillna('unknown', inplace=True)
Sign up to request clarification or add additional context in comments.

1 Comment

Oh God , I did not even realize, thanks a lot zipa !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.