0

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)
1
  • create a list(say lst=[]) outside the for loop and then append your dataframe in it.....then access your dataframe by lst[0],lst[1].....so where you stucked? Commented Jul 21, 2021 at 5:41

3 Answers 3

2
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)
Sign up to request clarification or add additional context in comments.

2 Comments

I executed the code. However, df_list is empty. Please note that 'files' variable is non-empty and contains 80 xls files
made an edit! make sure that you are able to read the data into your code(made changes to the file path as per windows)
0
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)

2 Comments

Ultimately, I wanted Excel output, but not this way. It creates many NaN values. In between different Pandas operations are involved. Now I am done. Math operations, Dataframe operations including horizontal concat are involved.
for this solution, I assumed that all the Data Frames had the same columns. If this is that is the case the code will work smoothly.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.