jonny Bravo
jonny Bravo

Reputation: 13

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'

Upvotes: 0

Views: 120

Answers (2)

Loochie
Loochie

Reputation: 2472

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)

Upvotes: 2

Nidal
Nidal

Reputation: 415

Replace your final loop with:

for f in files_xls:
    full_path = os.path.join(path, f)
    data = pd.read_excel(full_path) 
    df = df.append(data)

Upvotes: 1

Related Questions