1

I am doing some experiments and for each one I get an excel file. Every time I do a experiment I get between 20 and 30 excel files. This is why I try to don't import file by file using pd.read_excel

I'm trying to do a script to import each file into a DataFrame with the original name.

import os
import pandas as pd

path = r'C:\Luis\Desktop\Experiments\Day1\\'
files = os.listdir(path)
file = [x[:-5] for x in files] %delete extension

for f in file:
    f = pd.read_excel(path+f+'.xlsx', 'Analysis with Voltage')

I expect a DataFrame for each excel file

I got a DataFrame named f with the last excel file. I can see that the problem is that the variable don't iterate. Any suggestion about how to continue?

1
  • just use a list to keep the dataframes. ` data = [] for f in file: table = pd.read_excel(path+f+'.xlsx', 'Analysis with Voltage') data.append(table) Commented Jan 8, 2019 at 14:00

1 Answer 1

1
all_data = []
for f in file:
    all_data.append(pd.read_excel(path+f+'.xlsx', 'Analysis with Voltage'))

and if you want to get store dataframes with actual file name then:

all_data = dict()
for f in file:
    all_data[f] = pd.read_excel(path+f+'.xlsx', 'Analysis with Voltage')

This way you will have f as key for every DataFrame

Sign up to request clarification or add additional context in comments.

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.