I have multiple dataframes (e.g. df0, df1, df2, df3) that I need to iterate through, but the number of dataframes is arbitrary and is variable to change when I run this script on different datasets. I need to produce a list of these dataframes (using a loop):
dfs = [df0,df1,df2,df3]
So far this is what I have...
dfs = []
for i in range(4):
dfs.append('df'+str(i))
However, this returns:
dfs = ['df0', 'df1', 'df2', 'df3']
Is there a way to convert this list of strings into a list of dataframes? Or another way to write the loop, perhaps?
The goal is to create an excel document that is printable. I need to take a dataframe with 24 columns and any number of rows (from 5 entries to over 100). Because it needs to be printable, I have to split the dataframe into multiple dataframes, each with column labels and 3 rows per page, with one dataframe on each page. Since the number of dataframes I need to produce is dependent on the number of rows in the original dataframe, it involved producing the dataframes uniquely in a loop. Here's the code I used to produce the dataframes:
og = pd.ExcelFile(file).parse('Sheet1')
sheets = int(len(og)/3)
if (len(og)%3) != 0:
sheets = sheets + 1
frames = [og.loc[i*3:(3*i + 2)] for i in range(sheets)]
for i in range(len(frames)):
exec('df' +str(i)+ '= frames[i].transpose()')