Search code examples
pythonexcelpandasxlsx

How to Import Multiple excel file in PandasDataframe


I cannot load multiple excel files from a directory in only one Dataframe. I have tried two different ways and both do no work.

Gives me this error.

How can I solve the problem? It does find the files when creates the list, but than cannot open it in the Dataframe. Any hints ?

import pandas as pd
import os
import glob
import xlrd

cwd = os.getcwd()
cwd

path = '/Users/giovanni/Desktop/news media'
files = os.listdir(path)
files


files_xls = [f for f in files if f[-3:] == 'lsx']
files_xls





df = pd.DataFrame()

for f in files_xls:
    data = pd.read_excel(f)
    df = df.append(data)

FileNotFoundError: [Errno 2] No such file or directory: 'NOV.xlsx'

Solution

  • Try this:

    import os
    import glob
    path = '/Users/giovanni/Desktop/news media'
    df = pd.DataFrame()
    for file in glob.glob(os.path.join(path,'*.xlsx')):
        data = pd.read_excel(file)
        print(data)
        df = df.append(data)