简体   繁体   English

如何读取一个目录下的所有excel个文件为pandas dataframe

[英]How to read all excel files under a directory as a pandas dataframe

I have a couple of excel sheets (using pd.read_excel ) under a directory and would like to read them as a pandas and add them to a list.我在一个目录下有几张 excel 工作表(使用pd.read_excel ),我想将它们作为 pandas 读取并将它们添加到列表中。 so my list should end up having multiple dataframe in it.所以我的列表最终应该有多个 dataframe。 How can I do that?我怎样才能做到这一点?

Here is how I would do it.这是我将如何做到的。

import pandas as pd
import glob

# your path to folder containing excel files
datapath = "\\Users\\path\\to\\your\\file\\"

# set all .xls files in your folder to list
allfiles = glob.glob(datapath + "*.xls")

# for loop to aquire all excel files in folder
for excelfiles in allfiles:
    raw_excel = pd.read_excel(excelfiles)

# place dataframe into list
list1 = [raw_excel]

My method for this:我的方法是:

 data = os.listdir('data')
    df = pd.DataFrame()
    for file in data:
       path = 'data' + '/' + file
       temp = pd.read_excel(path)
       df = df.append(temp, ignore_index = True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM