简体   繁体   中英

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. so my list should end up having multiple dataframe in it. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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