简体   繁体   English

将文件夹中的多个excel文件读入pandas

[英]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模块通过pathlibunix匹配可以更轻松地完成此操作。

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')])

Breaking it down.打破它。

# 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.

相关问题 无法使用 Pandas 从一组文件夹中读取 excel 文件 - Unable to read excel files from set of folder using pandas 使用Python从文件夹中按顺序读取多个excel文件 - Read multiple excel files in order from folder with Python 使用 Pandas 从多个 Excel 文件中读取和合并数据 - Read & Combine Data From Multiple Excel Files with Pandas 将具有多个 excel 文件和多个选项卡的文件夹中的所有电子邮件提取到 pandas dataframe 中 Z23EEEB4347BDD2556DZ3EEEB4347BDD256BDZ - Extract all emails from a folder with multiple excel files and multiple tabs into a pandas dataframe in python Pandas 读取文件夹中的 excel 文件并将列取消透视到 Dataframe - Pandas read excel files in folder and Unpivot columns into Dataframe 如何将一个文件夹中的多个ann文件(来自brat注释)读入一个pandas dataframe? - How to read multiple ann files (from brat annotation) within a folder into one pandas dataframe? 如何将多个CSV文件从文件夹读取到以数据框名称作为文件名的熊猫中 - how to read multiple CSV files from folder into pandas with dataframe name as file name 使用 pandas 读取多个 excel 文件中的多个工作表 - Read multiple sheets in multiple excel files using pandas 如何快速读取多个excel文件,每个文件中都有多个工作表-熊猫? - How to quickly read in multiple excel files with multiple sheets in each - Pandas? 从文件夹中打开并读取多个xml文件 - Open and read multiple xml files from the folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM