简体   繁体   English

使用 python/pandas 将 dataframe 写入谷歌表格

[英]Writing a dataframe to google sheets using python/pandas

I am using google sheets to hold data for a shared project.我正在使用谷歌表格来保存共享项目的数据。 Using Google's Sheets API I access the data, process it in python, and I am trying to update the Sheets file using batchUpdate , in the function writer.使用 Google 的表格 API 我访问数据,在 python 中处理它,我正在尝试在 function 编写器中使用batchUpdate更新表格文件。

  • If I pass this function data as a list, it works as expected.如果我将此 function 数据作为列表传递,它将按预期工作。
  • If I pass a dataframe (as I would like to do) I get: TypeError: Object of type DataFrame is not JSON serializable If I pass a dataframe (as I would like to do) I get: TypeError: Object of type DataFrame is not JSON serializable
  • If I use .to_json() , I get this:如果我使用.to_json() ,我会得到:

googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/XXX/values:batchUpdate?alt=json returned "Invalid value at 'data[0].values' (type.googleapis.com/google.protobuf.ListValue), "{"0":{"0":1},"1":{"0":2},"2":{"0":3},"3":{"0":4}}"". googleapiclient.errors.HttpError: <HttpError 400 当请求https://sheets.googleapis.com/v4/spreadsheets/XXX/values:batchUpdate?alt=json返回“'data[0].values' 处的值无效(类型。 googleapis.com/google.protobuf.ListValue), "{"0":{"0":1},"1":{"0":2},"2":{"0":3}," 3":{"0":4}}""。 Details: "[{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'data[0].values', 'description': 'Invalid value at 'data[0].values' (type.googleapis.com/google.protobuf.ListValue), "{"0":{"0":1},"1":{"0":2},"2":{"0":3},"3":{"0":4}}"'}]}]">详细信息:“[{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'data[0].values', 'description': '无效值在“数据[0].values”(type.googleapis.com/google.protobuf.ListValue),“{“0”:{“0”:1},“1”:{“0”:2}, 2":{"0":3},"3":{"0":4}}"'}]}]">

Any pointers would be much appreciated.任何指针将不胜感激。

import pickle
import os.path
import pandas as pd
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from pprint import pprint

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']

# Spreadsheet ID: https://docs.google.com/spreadsheets/d/XXX/edit#gid=0
SPREADSHEET_ID = 'XXX'
RANGE_NAME = 'contacts'

def writer(df):
    service = build('sheets', 'v4', credentials=gsheet_api(SCOPES))
    sheet_name = 'contacts'
    data = [{'range' : sheet_name, 'values' : df}]
    batch_update_values_request_body = {
        'value_input_option': 'RAW',
        'data': data }

    request = service.spreadsheets().values().batchUpdate(spreadsheetId=SPREADSHEET_ID,
                                                          body=batch_update_values_request_body)
    response = request.execute()
    pprint(response)

df = [[1, 2, 3, 4]]
writer(df)

I believe your goal and situation as follows.我相信你的目标和情况如下。

  • You want to put the dataframe to Google Spreadsheet using googleapis with Python.您想使用带有 Python 的 googleapis 将 dataframe 放入 Google 电子表格。
  • You have already been able to get and put values for Google Spreadsheet using Sheets API.您已经能够使用表格 API 获取和放置 Google 电子表格的值。

For this, how about this answer?为此,这个答案怎么样?

Modification points:修改点:

  • I'm not sure about the values of the dataframe.我不确定 dataframe 的值。 So in this answer, I would like to explain about the modification points using the following sample dataframe.所以在这个答案中,我想使用以下示例 dataframe 来解释修改点。

     AB C 0 1 2 3 1 4 5 6 2 7 8 9
  • Unfortunately, the dataframe cannot be directly used for the request body of the method "spreadsheets.values.batchUpdate".不幸的是,dataframe 不能直接用于“spreadsheets.values.batchUpdate”方法的请求体。 So in this case, it is required to convert from the dataframe to the 2 dimensional array.所以在这种情况下,需要将 dataframe 转换为二维数组。 For this, I used tolist() .为此,我使用tolist()

When your script is modified using the sample dataframe, it becomes as follows.当您的脚本使用示例 dataframe 修改时,它变为如下。

Modified script:修改后的脚本:

From: 从:
 df = [[1, 2, 3, 4]] writer(df)
To: 至:
 sampleValue = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] df = pd.DataFrame(sampleValue, columns=list('ABC')) values = [df.columns.values.tolist()] values.extend(df.values.tolist()) writer(values)

Note:笔记:

  • If you don't want to include the header row, please modify as follows.如果不想包含 header 行,请进行如下修改。
    • From

       values = [df.columns.values.tolist()] values.extend(df.values.tolist())
    • To

       values = df.values.tolist()

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM