0

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?

HHH
  • 5,485
  • 17
  • 84
  • 154
  • What code do you have so far? You could just get your current directory, look for excel file, read them to df, put them in list. – MyNameIsCaleb Apr 17 '19 at 15:50
  • 1
    1. Please follow the guide for good questions -> where is your code what have you tried so far what error did you encounter – Marvin Taschenberger Apr 17 '19 at 15:51
  • 2. then we might help you better. 3, take a look into `pathlib` and `Path` and it's `glob` method and how `list comprehension` works – Marvin Taschenberger Apr 17 '19 at 15:51
  • Try this answer. https://stackoverflow.com/questions/20908018/import-multiple-excel-files-into-python-pandas-and-concatenate-them-into-one-dat – run-out Apr 17 '19 at 16:01
  • duplicate of https://stackoverflow.com/questions/20906474/import-multiple-csv-files-into-pandas-and-concatenate-into-one-dataframe – VnC Apr 18 '19 at 17:28

2 Answers2

0

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]
Angel Roman
  • 518
  • 1
  • 3
  • 13
0

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)
keramat
  • 3,303
  • 5
  • 21
  • 33