Search code examples
pythongoogle-sheetsgoogle-colaboratorygoogle-sheets-apigspread

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


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())

Solution

  • 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())