python - Multiple excel files -


i want loop through directory , find specific xlsx files , put them each separate pandas dataframe. thing here want sheets in excel files in dataframe.

below sample of code implemented, need add logic pick sheets:

import pandas pd glob import glob  path = 'path_to_file'  files = glob(path + '/*file*.xlsx')  get_df = lambda f: pd.read_excel(f)  dodf = {f: get_df(f) f in files}  dodf[files[2]] --- dictionary of dataframes  

as described in this answer in pandas still have access excelfile class, loads file creating object.

this object has .sheet_names property gives list of sheet names in current file.

xl = pd.excelfile('foo.xls') xl.sheet_names  # list of sheet names 

to handle import of specific sheet, use .parse(sheet_name) on object of imported excel file:

xl.parse(sheet_name)  # read specific sheet dataframe 

for code like:

get_df = lambda f: pd.excelfile(f) dodf = {f: get_df(f) f in files} 

...gives dodf dictionary of excelfile objects.

filename = 'yourfilehere.xlsx' a_valid_sheet = dodf[filename].sheet_names[0] # first sheet df = dodf[filename].parse(sheet_name) 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -