3

I am trying to parse multiple excel sheets with Pandas into separate individual DataFrames.

My code so far is:

sheet_names =[tab1, tab2]
df_names = [1,2]

def initilize_dataframes(sheet_names):
    for name in sheet_names:
       df = xls_file.parse(name) #parse the xlxs sheet
       df = df.transpose() #transpose dates to index
       new_header = df.iloc[0] #column header names 
       df = df[1:] #drop 1st row 
       df.rename(columns=new_header, inplace= True) #rename the columns
    return df`
`
for i in df_names:
     df_(i) = initilize_dataframes(sheet_names)#something like this idk 

The last two lines I can not wrap my head around. I get that the function will return the df, but I would like it to take the values from the df_names list. And label the DataFrame accordingly.

For example, tab1 in the excel sheet the DataFrame should be named df_1 and looping for tab2 and df_2 respectively.

1 Answer 1

1

It is possible by globals:

for i, val in enumerate(df_names):
     globals()['df_' + str(vals)] = initilize_dataframes(sheet_names[i])

But better is use dict of DataFrames, sheet_names select by positions from enumerate, but need substract 1, because python counts from 0:

dfs = {}
for i, val in enumerate(df_names):
     dfs[val] = initilize_dataframes(sheet_names[i])

print (dfs[1])
Sign up to request clarification or add additional context in comments.

8 Comments

Worked like a charm, thank you ! and the dictionary was something i hadn't even considered. Brilliant.
@KKobain - Glad can help!
What if my DataFrames are not integers, but rather df_names= ['sheet1','sheet2','sheet3']
And then select by print (dfs['sheet1'])
Now its not working ? its showing this error raise XLRDError('No sheet named <%r>' % sheet_name) XLRDError: No sheet named <'t'> so it doesnt seem to be iterating correctly. Is there something wrong with the initial definition ?
|

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.