Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.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 将多个类似的CSV文件连接到一个大数据帧中_Python_Csv_Pandas - Fatal编程技术网

Python 将多个类似的CSV文件连接到一个大数据帧中

Python 将多个类似的CSV文件连接到一个大数据帧中,python,csv,pandas,Python,Csv,Pandas,我有一个目录,其中只有我想要使用的CSV文件。我想连接所有这些CSV文件并创建一个更大的文件。我试过一个代码,但不起作用 import os import pandas as pd targetdir = r'C:/Users/toshiba/Documents/ICF2011/Base Admision San Marcos 2014-2/Sabado' filelist = os.listdir(targetdir) big_df=pd.DataFrame() for file in

我有一个目录,其中只有我想要使用的CSV文件。我想连接所有这些CSV文件并创建一个更大的文件。我试过一个代码,但不起作用

import os
import pandas as pd

targetdir = r'C:/Users/toshiba/Documents/ICF2011/Base Admision San Marcos 2014-2/Sabado'

filelist = os.listdir(targetdir) 

big_df=pd.DataFrame()
for file in filelist :
    big_df.append(pd.read_csv(file), ignore_index=True)
我运行了代码,有一条消息说:
IOError:A011.csv文件不存在

这是矛盾的,因为
A011.csv
是我使用的目录中的第一个csv文件。

listdir
只返回文件名,而不返回完整路径。要获得完整的路径,您需要加入
targetdir
file
(变量名不正确,因为它掩盖了
文件的类型)。此外,您还必须捕获
.append
的结果,因为它返回一个新对象,而不是原地追加

for filename in filelist:
    big_df = big_df.append(pd.read_csv(os.path.join(targetdir, filename), ignore_index=True)

listdir
只返回文件名,不返回完整路径。要获得完整的路径,您需要加入
targetdir
file
(变量名不正确,因为它掩盖了
文件的类型)。此外,您还必须捕获
.append
的结果,因为它返回一个新对象,而不是原地追加

for filename in filelist:
    big_df = big_df.append(pd.read_csv(os.path.join(targetdir, filename), ignore_index=True)

正如在另一个答案中提到的,您需要使用完整路径,而不是本地路径

我建议使用而不是,因为这样您不会制作很多中间帧:

big_df = pd.concat(pd.read_csv(os.path.join(targetdir, filename),
                               ignore_index=True)
                   for filename in filelist)

正如在另一个答案中提到的,您需要使用完整路径,而不是本地路径

我建议使用而不是,因为这样您不会制作很多中间帧:

big_df = pd.concat(pd.read_csv(os.path.join(targetdir, filename),
                               ignore_index=True)
                   for filename in filelist)