Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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 从目录中读取所有excel文件,而不是单独列出它们_Python_Pandas - Fatal编程技术网

Python 从目录中读取所有excel文件,而不是单独列出它们

Python 从目录中读取所有excel文件,而不是单独列出它们,python,pandas,Python,Pandas,我制作了一个程序,根据excel文件的具体文件名来合并这些文件[4],但如果我想合并特定目录中列出的所有文件(比如我桌面上一个名为test的文件夹),我该怎么做呢 import pandas as pd import os os.chdir("/users/me/desktop/test") excel_names = ["/users/me/desktop/test/test1.xlsx", "/users/me/desktop/test/test2.xlsx"] excels

我制作了一个程序,根据excel文件的具体文件名来合并这些文件[4],但如果我想合并特定目录中列出的所有文件(比如我桌面上一个名为test的文件夹),我该怎么做呢

 import pandas as pd
 import os 
 os.chdir("/users/me/desktop/test") 
 excel_names = ["/users/me/desktop/test/test1.xlsx", "/users/me/desktop/test/test2.xlsx"]
 excels = [pd.ExcelFile(name) for name in excel_names]

 frames = [pd.read_excel(x, header=None,index_col=None) for x in excels]
 frames[1:] = [df[1:] for df in frames[1:]]

 combined = pd.concat(frames)

 combined.to_excel("combine.xlsx", header=False, index=False)

根据您的具体标准,大致如下:

df = pd.concat([pd.read_excel(i, header=None, index_col=None) for i in os.listdir() if i.endswith('.xlsx')])

首先,您确定需要这样做吗?如果您只是使用
myscript/path/to/sheets/*.xls
运行脚本,shell将把glob模式转换为所有匹配文件的列表,您的代码将不知道或不关心它的参数是来自您列出的特定名称还是您使用的glob模式。