user9003011
user9003011

Reputation: 306

How to read multiple excel files in Different Pandas Dataframes

I have collection of excel files containing similar datasets. I want it to be read by different Pandas dataframes.

import glob
import pandas as pd

path=r"C:users/me/desktop/ExcelData"

files=glob.glob('*.xls')
for f in files:
      df[f]=pd.read_excel(f)

Upvotes: 0

Views: 2132

Answers (3)

Joaquim
Joaquim

Reputation: 440

This reads multiple excel files into a directory into separate dataframes. The dataframes are named after the source excel file name.

upload_path = "../test_files/sample"
files = os.listdir(upload_path)
files_xls = [f for f in files if f[-3:] == 'xls']
for f in files_xls:
    dataframe_name = f[:-4]
    dataframe_name = pd.read_excel(os.path.join(upload_path,f), header=1)

Upvotes: 0

JJ Moradel
JJ Moradel

Reputation: 19

import pandas as pd
import os
import glob

path = r"C:users/me/desktop/ExcelData"
files = glob.glob(path + "\*.xls")

finalexcelsheet = pd.DataFrame()

for file in files:
    df = pd.concat(pd.read_excel(file, sheet_name = None), ignore_index=True, 
    sort=False)
    finalexcelsheet = finalexcelsheet.append(df, ignore_index = True)

print(finalexcelsheet)

Upvotes: 0

mac_online
mac_online

Reputation: 370

import glob
import pandas as pd
import os

path=r"C:\\users\\me\\desktop\\ExcelData\\"
csv_files = glob.glob(os.path.join(path, "*.xls"))

dfl=[]
for f in csv_files:
    x= pd.read_excel(f)
    dfl.append(x)

Upvotes: 2

Related Questions