Saturday, 11 January 2020

saving all excel sheets as pandas dataframe

I have a dictionary that stores all the dataframes from a given excel file.

import pandas as pd
xl=pd.ExcelFile('pandas_worksheets.xlsx')

mydic={}
for i in xl.sheet_names:
    mydic[i] = xl.parse(i)

I can call any dataframe sheet using this syntax:

mydic['olympic']

But how do I simply call "olympic" to return that dataframe? (without using mydic)

somthing like this works...

olympic = xl.parse('olympic')

But the same logic does not work in the for loop

for i in xl.sheet_names:
    i = xl.parse(i)

It saves the last dataframe as literal i instead of replacing it with sheet-name.


update:

mydic={'a':1, 'b': 2}

for i in mydic:
    i = mydic[i]

a

NameError: name 'a' is not defined

(expected value 1 instead of NameError)



from saving all excel sheets as pandas dataframe

No comments:

Post a Comment