Home > OS >  Append a dataframe to an existing google worksheet with that has data without overwriting the data
Append a dataframe to an existing google worksheet with that has data without overwriting the data

Time:03-10

I have an existing worksheet that has data in one column that I have populated from a data frame. I want to take a different data frame and populate the second column in the worksheet but struggling to understand how to do it. What ends up happening is that it overwrites the data in the first column.

My column in the worksheet currently looks like this

Ilastik
0.1111082937
0.1744560321
0.1370938026
0.1028610827
0.6465271181
0.1871931638
0.1105310022
0.2431738711
0.4100191368
0.4919283379

I want it to look like

Ilastik         Kmeans
0.1111082937    0.473713
0.1744560327    0.359408
0.1370938026    0.368002
0.1028610827    0.219979

Both data have come from different dataframes. I opened my worksheet using

worksheet_hemp_kmeans = sh.worksheet("Hemp Model Results")

And then tried adding the dataframe to it. hemp_kmeans_df2 is my dataframe that I want to add.

worksheet_hemp_kmeans.update([hemp_kmeans_df2.columns.values.tolist()]  hemp_kmeans_df2.values.tolist())

CodePudding user response:

I believe your goal is as follows.

  • You want to put the values of [hemp_kmeans_df2.columns.values.tolist()] hemp_kmeans_df2.values.tolist() to the column "B" of the sheet "Hemp Model Results" using gspread for python.

In this case, how about the following modification?

Modified script:

values = [hemp_kmeans_df2.columns.values.tolist()]   hemp_kmeans_df2.values.tolist()
sh.values_update("'Hemp Model Results'!B1", params={'valueInputOption': 'USER_ENTERED'}, body={'values': values})
  • By this modification, values is put to the column "B" of "Hemp Model Results" sheet.

  • From worksheet_hemp_kmeans = sh.worksheet("Hemp Model Results"), I guessed that your sh is Spreadsheet Class.

Reference:

CodePudding user response:

Thank you I managed to work it by doing

worksheet_hemp_kmeans.update("B1", [hemp_kmeans_df2.columns.values.tolist()]  hemp_kmeans_df2.values.tolist())
  • Related