Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用循环将数据帧附加到现有的空数据帧?_Python_Csv - Fatal编程技术网

Python 如何使用循环将数据帧附加到现有的空数据帧?

Python 如何使用循环将数据帧附加到现有的空数据帧?,python,csv,Python,Csv,我想使用循环用csv文件中的数据填充一个空数据框。 除了每次数据帧只记录最后一个csv文件而不是目标文件夹中存在的所有csv文件外,代码工作正常 我做错什么了吗 下面是我的一段代码: 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 =

我想使用循环用csv文件中的数据填充一个空数据框。 除了每次数据帧只记录最后一个csv文件而不是目标文件夹中存在的所有csv文件外,代码工作正常

我做错什么了吗

下面是我的一段代码:

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)
只需将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)

哦,天哪,我都没意识到,非常感谢zipa!