Python Set Data Frame Columns to Google Sheet Cell Range












0














I am working on taking my the results of my regression analysis and pushing them to cells in a Google Sheet (Using gspread). For this process, I need to query a range within my Google Sheet and then set the individual cell values to the rows in each column in a data frame.



Here is the code storing the Google Sheet range:



worksheet = regression_output_sheet.worksheet("df_full-raw")
# worksheet.range(row_start, column_start, row_end, column_end)
worksheet_range = worksheet.range(1, 1, 500, 3)
print(worksheet_range)


Cell format:



# worksheet_range format
R = Row
C = Column
'' = Value
<Cell R#C# ''>

# worksheet_range array
[<Cell R1C1 ''>, <Cell R2C1 ''>, ..., <Cell R500C3 ''>]


Here is my data frame:



# df_full data frame

date b_clicks b_cpc
2 2018-01-01 72 2.43
3 2018-01-02 232 2.80
...
325 2018-11-20 162 4.31

[324 rows x 3 columns]


Since the worksheet_range array is comma separated by row range an then column range, I thought the best approach to setting the data fram values to the cell values was by looping through the length of columns selected (3 in this e.g.), setting that to an array, which would then be fed into a loop for worksheet.range(1, i, 500, i). From there I have a loop that loops through each column in the data frame and sets the values to a list and then for each row in the worsheet_range the value is set to each row in the data frame column range and then calling update_cells, but I don't think my loop structure is correct because it is looping R1C1 - R1C500 a ton of times and setting the values for each column to the start of those loops.



This is the desired output of the worksheet_range:



[
<Cell R1C1 '2018-01-01'>, <Cell R2C1 '2018-01-02'>,
..., <Cell R1C2 '72'>, <Cell R2C2 '232'>,
..., <Cell R1C3 '2.43'>, <Cell R2C3 '2.80'>,
..., <Cell R500C3 '4.31'>
]


Full Code:



# store count of column names
gs_columns =

# count columns
for i in range(0,len(columns)):
gs_columns.append(i+1)

print(gs_columns) # [1,2,3]

# for each column, store a worksheet range
for col_val in gs_columns:
worksheet_range = worksheet.range(1, col_val, 500, col_val)
print(type(worksheet_range))
for col_name in columns:
individual_data_frame = df_full[col_name].values.tolist()
print(individual_data_frame)
# for each row in that range, set the row equal to each value in the dataframe column
for row in range(len(worksheet_range)):
worksheet_range[row].value = individual_data_frame[row]
for arr in worksheet_range:
print(arr)

# update cells
worksheet.update_cells(worksheet_range)









share|improve this question



























    0














    I am working on taking my the results of my regression analysis and pushing them to cells in a Google Sheet (Using gspread). For this process, I need to query a range within my Google Sheet and then set the individual cell values to the rows in each column in a data frame.



    Here is the code storing the Google Sheet range:



    worksheet = regression_output_sheet.worksheet("df_full-raw")
    # worksheet.range(row_start, column_start, row_end, column_end)
    worksheet_range = worksheet.range(1, 1, 500, 3)
    print(worksheet_range)


    Cell format:



    # worksheet_range format
    R = Row
    C = Column
    '' = Value
    <Cell R#C# ''>

    # worksheet_range array
    [<Cell R1C1 ''>, <Cell R2C1 ''>, ..., <Cell R500C3 ''>]


    Here is my data frame:



    # df_full data frame

    date b_clicks b_cpc
    2 2018-01-01 72 2.43
    3 2018-01-02 232 2.80
    ...
    325 2018-11-20 162 4.31

    [324 rows x 3 columns]


    Since the worksheet_range array is comma separated by row range an then column range, I thought the best approach to setting the data fram values to the cell values was by looping through the length of columns selected (3 in this e.g.), setting that to an array, which would then be fed into a loop for worksheet.range(1, i, 500, i). From there I have a loop that loops through each column in the data frame and sets the values to a list and then for each row in the worsheet_range the value is set to each row in the data frame column range and then calling update_cells, but I don't think my loop structure is correct because it is looping R1C1 - R1C500 a ton of times and setting the values for each column to the start of those loops.



    This is the desired output of the worksheet_range:



    [
    <Cell R1C1 '2018-01-01'>, <Cell R2C1 '2018-01-02'>,
    ..., <Cell R1C2 '72'>, <Cell R2C2 '232'>,
    ..., <Cell R1C3 '2.43'>, <Cell R2C3 '2.80'>,
    ..., <Cell R500C3 '4.31'>
    ]


    Full Code:



    # store count of column names
    gs_columns =

    # count columns
    for i in range(0,len(columns)):
    gs_columns.append(i+1)

    print(gs_columns) # [1,2,3]

    # for each column, store a worksheet range
    for col_val in gs_columns:
    worksheet_range = worksheet.range(1, col_val, 500, col_val)
    print(type(worksheet_range))
    for col_name in columns:
    individual_data_frame = df_full[col_name].values.tolist()
    print(individual_data_frame)
    # for each row in that range, set the row equal to each value in the dataframe column
    for row in range(len(worksheet_range)):
    worksheet_range[row].value = individual_data_frame[row]
    for arr in worksheet_range:
    print(arr)

    # update cells
    worksheet.update_cells(worksheet_range)









    share|improve this question

























      0












      0








      0


      1





      I am working on taking my the results of my regression analysis and pushing them to cells in a Google Sheet (Using gspread). For this process, I need to query a range within my Google Sheet and then set the individual cell values to the rows in each column in a data frame.



      Here is the code storing the Google Sheet range:



      worksheet = regression_output_sheet.worksheet("df_full-raw")
      # worksheet.range(row_start, column_start, row_end, column_end)
      worksheet_range = worksheet.range(1, 1, 500, 3)
      print(worksheet_range)


      Cell format:



      # worksheet_range format
      R = Row
      C = Column
      '' = Value
      <Cell R#C# ''>

      # worksheet_range array
      [<Cell R1C1 ''>, <Cell R2C1 ''>, ..., <Cell R500C3 ''>]


      Here is my data frame:



      # df_full data frame

      date b_clicks b_cpc
      2 2018-01-01 72 2.43
      3 2018-01-02 232 2.80
      ...
      325 2018-11-20 162 4.31

      [324 rows x 3 columns]


      Since the worksheet_range array is comma separated by row range an then column range, I thought the best approach to setting the data fram values to the cell values was by looping through the length of columns selected (3 in this e.g.), setting that to an array, which would then be fed into a loop for worksheet.range(1, i, 500, i). From there I have a loop that loops through each column in the data frame and sets the values to a list and then for each row in the worsheet_range the value is set to each row in the data frame column range and then calling update_cells, but I don't think my loop structure is correct because it is looping R1C1 - R1C500 a ton of times and setting the values for each column to the start of those loops.



      This is the desired output of the worksheet_range:



      [
      <Cell R1C1 '2018-01-01'>, <Cell R2C1 '2018-01-02'>,
      ..., <Cell R1C2 '72'>, <Cell R2C2 '232'>,
      ..., <Cell R1C3 '2.43'>, <Cell R2C3 '2.80'>,
      ..., <Cell R500C3 '4.31'>
      ]


      Full Code:



      # store count of column names
      gs_columns =

      # count columns
      for i in range(0,len(columns)):
      gs_columns.append(i+1)

      print(gs_columns) # [1,2,3]

      # for each column, store a worksheet range
      for col_val in gs_columns:
      worksheet_range = worksheet.range(1, col_val, 500, col_val)
      print(type(worksheet_range))
      for col_name in columns:
      individual_data_frame = df_full[col_name].values.tolist()
      print(individual_data_frame)
      # for each row in that range, set the row equal to each value in the dataframe column
      for row in range(len(worksheet_range)):
      worksheet_range[row].value = individual_data_frame[row]
      for arr in worksheet_range:
      print(arr)

      # update cells
      worksheet.update_cells(worksheet_range)









      share|improve this question













      I am working on taking my the results of my regression analysis and pushing them to cells in a Google Sheet (Using gspread). For this process, I need to query a range within my Google Sheet and then set the individual cell values to the rows in each column in a data frame.



      Here is the code storing the Google Sheet range:



      worksheet = regression_output_sheet.worksheet("df_full-raw")
      # worksheet.range(row_start, column_start, row_end, column_end)
      worksheet_range = worksheet.range(1, 1, 500, 3)
      print(worksheet_range)


      Cell format:



      # worksheet_range format
      R = Row
      C = Column
      '' = Value
      <Cell R#C# ''>

      # worksheet_range array
      [<Cell R1C1 ''>, <Cell R2C1 ''>, ..., <Cell R500C3 ''>]


      Here is my data frame:



      # df_full data frame

      date b_clicks b_cpc
      2 2018-01-01 72 2.43
      3 2018-01-02 232 2.80
      ...
      325 2018-11-20 162 4.31

      [324 rows x 3 columns]


      Since the worksheet_range array is comma separated by row range an then column range, I thought the best approach to setting the data fram values to the cell values was by looping through the length of columns selected (3 in this e.g.), setting that to an array, which would then be fed into a loop for worksheet.range(1, i, 500, i). From there I have a loop that loops through each column in the data frame and sets the values to a list and then for each row in the worsheet_range the value is set to each row in the data frame column range and then calling update_cells, but I don't think my loop structure is correct because it is looping R1C1 - R1C500 a ton of times and setting the values for each column to the start of those loops.



      This is the desired output of the worksheet_range:



      [
      <Cell R1C1 '2018-01-01'>, <Cell R2C1 '2018-01-02'>,
      ..., <Cell R1C2 '72'>, <Cell R2C2 '232'>,
      ..., <Cell R1C3 '2.43'>, <Cell R2C3 '2.80'>,
      ..., <Cell R500C3 '4.31'>
      ]


      Full Code:



      # store count of column names
      gs_columns =

      # count columns
      for i in range(0,len(columns)):
      gs_columns.append(i+1)

      print(gs_columns) # [1,2,3]

      # for each column, store a worksheet range
      for col_val in gs_columns:
      worksheet_range = worksheet.range(1, col_val, 500, col_val)
      print(type(worksheet_range))
      for col_name in columns:
      individual_data_frame = df_full[col_name].values.tolist()
      print(individual_data_frame)
      # for each row in that range, set the row equal to each value in the dataframe column
      for row in range(len(worksheet_range)):
      worksheet_range[row].value = individual_data_frame[row]
      for arr in worksheet_range:
      print(arr)

      # update cells
      worksheet.update_cells(worksheet_range)






      python for-loop gspread






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 15:38









      cphill

      1,66163374




      1,66163374
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415571%2fpython-set-data-frame-columns-to-google-sheet-cell-range%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415571%2fpython-set-data-frame-columns-to-google-sheet-cell-range%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'