[英]Read multiple excel files from a folder into pandas
I would like to read several excel files contained into a folder in the Desktop of my MacBook into pandas.我想将包含在我的 MacBook 桌面文件夹中的几个 excel 文件读入熊猫。
The folder in the desktop is contains a folder (project dataset) with all the excel files and the Jupiter notebook page where I am writing the code (draft progetto)桌面中的文件夹包含一个文件夹(项目数据集),其中包含所有 excel 文件和我正在编写代码的 Jupiter 笔记本页面(草稿 progetto)
I wrote the following code:我写了以下代码:
path = os.getcwd()
files = os.listdir(path)
files
Output:输出:
['.DS_Store', 'draft progetto.ipynb', '.ipynb_checkpoints', 'project_dataset']
Then when I run:然后当我运行时:
files_xls = [f for f in files if f[3:] == 'xlsx']
files_xls
I GET AN EMPTY LIST AS OUTPUT!!我得到一个空列表作为输出!! WHY IS THIS?
为什么是这样?
IIUC,国际大学联盟,
this is something that can be done much easier with pathlib
and unix
matching using the glob
module.使用
glob
模块通过pathlib
和unix
匹配可以更轻松地完成此操作。
from pathlib import Path
import pandas as pd
#one liner
your_path = 'path_to_excel_files'
df = pd.concat([pd.read_excel(f) for f in Path(your_path).rglob('*.xlsx')])
# find the excel files
# if you want to change the path do Path('your_path')...
files = [file for file in Path.cwd.rglob('*.xlsx')]
#create a list of dataframes.
dfs_list = [pd.read_excel(file) for file in files])
#concat
df = pd.concat(dfs_list)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.