1

I have an Excel files with multiple sheets named: a,b,c,d,e.....,z I can read sheet a using following code

xl=pd.ExcelFile(r'path.xlsx')
a=xl.parse('a')

How can I assign the names of sheets:a,b,c...,z as dataframe names so that I can easily call it later

ducvu169
  • 103
  • 1
  • 12

1 Answers1

0

You can create dict of DataFrames by dict comprehension:

xl = pd.ExcelFile(r'path.xlsx')
print (xl.parse('a'))
   a  b
0  1  2

print (xl.sheet_names)
['a', 'b', 'c']

dfs = {sheet: xl.parse(sheet) for sheet in xl.sheet_names}
print (dfs)
{'a':    a  b
0  1  2, 'b':    b  c
0  5  5, 'c':    r  t
0  7  8}

print (dfs['a'])
   a  b
0  1  2

print (dfs['b'])
   b  c
0  5  5
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252