简体   繁体   中英

Read multiple excel files to multiple variables (variable name from the file itself)

I have multiple excel files:

import os
files = os.listdir()

#list excel files in the folder
files_xlsx = [f for f in files if f[-4:] == 'xlsx']

#sort
files_xlsx.sort()

#remove the extension
for i in range(len(files_xlsx)):
    files_xlsx[i] = files_xlsx[i][:-5]

files_xlsx
['Microsoft_Excel_Worksheet',
'Microsoft_Excel_Worksheet1',
'slide2_chart_rId3_object_rId1',
'slide3_chart_rId2_object_rId1',
'slide3_chart_rId3_object_rId1',
'slide4_chart_rId2_object_rId1',
'slide4_chart_rId3_object_rId1',
'slide5_chart_rId3_object_rId1',
'slide6_chart_rId2_object_rId1']

I'd like to read the files using pandas and save each dataframe to a variable:

import pandas as pd

??? how to loop this ???
Microsoft_Excel_Worksheet = pd.read_excel(files_xlsx[0] + '.xlsx'), index_col='Unnamed: 0')
Microsoft_Excel_Worksheet1 = pd.read_excel(files_xlsx[1] + '.xlsx'), index_col='Unnamed: 0')
slide2_chart_rId3_object_rId1 = pd.read_excel(files_xlsx[2] + '.xlsx'), index_col='Unnamed: 0')

I don't know how to loop the procedure. Thanks in advance for the help!

what you can do is read xlsx files to dataframes and append to a combined list

basepath = <basepath>
files = list(filter(lambda x: '. xlsx' in x, os.listdir(basepath)))
alldf = []
for f in files:
    df= pd.read_excel(f"{basepath}/{f}",index_col='Unnamed: 0')
    alldf.append(df)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM