0

I have a data frame of 100 rows that I am trying to split into multiple Dataframes as per the below code:

for m in df['month'].unique():
    temp = 'df_{}'.format(m) 
    vars()[temp] = finance_metrics[df['month']==m]

This gives me 5 new Dataframes as below:

df_January
df_February
df_March
df_April
df_May

I am trying to have each of these 5 Dataframe inserted into a Google sheet which works well as per the code below. The issue I am having is I am trying to have each of these Dataframes inserted one below the other in the Google Sheet and I am having trouble trying to have the cell referenced accordingly. The below code inserted each Dataframe starting cell A4 and it so happens that the last Dataframe that is created in a loop using the above code overwrites all of the previously created Dataframe. I would like to avoid the Dataframes getting overwritten.

wks.set_dataframe(vars()[temp], 'A'+'4+len(vars()[temp])')
Kevin Nash
  • 1,511
  • 3
  • 18
  • 37

1 Answers1

0

This should work. You can pass cell address as a tuple too.

offset = 0
for m in df['month'].unique():
  temp = finance_metrics[df['month']==m]
  wks.set_dataframe(temp, (4+offset, 1))
  offset += len(temp)
Nithin
  • 5,470
  • 37
  • 44