0
path = '/Desktop/somefolder'
for filename in os.listdir(path):
    with open(path+filename) as f:
         - read the 3-4 excel files and attach the path
         - be able to concat them based on a specific column

filename gives me the name of the file I have in the directory. My idea was to concat the filename with the path to be able to read and concat them.

I am not sure how to use the filename that I get to be able to load it as a df and concat it.

4
  • Have you considered using pd.read_excel yet? Commented Nov 10, 2022 at 19:34
  • Yes, that isn't working. Commented Nov 10, 2022 at 19:41
  • Possible duplicate stackoverflow.com/questions/28405182/… Commented Nov 10, 2022 at 19:44
  • No, the link does not resolve this issue. @Greg Commented Nov 10, 2022 at 19:46

2 Answers 2

1

Try:

import pandas as pd
import os

path = '/Desktop/somefolder/'

dfs = []
for filename in os.listdir(path):
    dfs.append(pd.read_excel(path+filename, engine='openpyxl'))

pd.concat(dfs, axis=0)
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting an error : --> Unknown engine: <module 'openpyxl' from '/opt/anaconda3/envs/union_rates_time_series/lib/python3.10/site-packages/openpyxl/__init__.py'> @user16367225
Try installing conda install -c anaconda openpyxl in Anaconda Prompt in your environment. Make sure you install in it in the right environment.
1

Read the data into data frames

df1 = pd.read_excel('file1.xlsx')
df2 = pd.read_excel('file2.xlsx') 

Create filtered data frames

df1Filtered = df1[df1["YourColumnName"].("YourColumnValues")
df2Filtered = df2[df2["YourColumnName"].("YourColumnValues")

Concat the filtered data frames

NewDF = pd.concat([df1Filtered, df2Filtered])

Write a new file to excel

NewDF.to_excel('NewFile.xlsx')

2 Comments

Thank you for the solution, but this starts to get ugly if you have 15 to 16 excel files...
You're welcome, and that is subjective, original post said 3-4 excel files so your mileage may vary.

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.