Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

Mentioning cell where Pandas Dataframe gets inserted in Google Sheet

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])')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...