Return multiple DataFrames from a function with Pandas

Viewed 7244

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 Answers
Related