python - Return multiple DataFrames from a function with Pandas -
i trying parse multiple excel sheets pandas separate individual dataframes.
my code far is:
sheet_names =[tab1, tab2] df_names = [1,2] def initilize_dataframes(sheet_names): name in sheet_names: df = xls_file.parse(name) #parse xlxs sheet df = df.transpose() #transpose dates index new_header = df.iloc[0] #column header names df = df[1:] #drop 1st row df.rename(columns=new_header, inplace= true) #rename columns return df` ` in df_names: df_(i) = initilize_dataframes(sheet_names)#something idk
the last 2 lines can not wrap head around. function return df, take values df_names list. , label dataframe accordingly.
for example, tab1 in excel sheet dataframe should named df_1 , looping tab2 , df_2 respectively.
it possible globals
:
for i, val in enumerate(df_names): globals()['df_' + str(vals)] = initilize_dataframes(sheet_names[i])
but better use dict
of dataframes
, sheet_names
select positions enumerate
, need substract 1
, because python counts 0
:
dfs = {} i, val in enumerate(df_names): dfs[val] = initilize_dataframes(sheet_names[i]) print (dfs[1])
Comments
Post a Comment