简体   繁体   English

如何通过循环将数据框追加到现有的空数据框?

[英]How to append a dataframe to an existing empty dataframe with a loop?

I want to fill an empty dataframe with data in csv files by using a loop. 我想通过循环使用csv文件中的数据填充一个空的数据框。 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. 该代码工作正常,除了每次数据框仅记录最后一个csv文件,而不记录目标文件夹中存在的所有csv文件。

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)

Just move newDF outside the loop: 只需将newDF循环即可:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM