小编Ham*_*ton的帖子

使用 python/pandas 将数据框写入谷歌工作表

我正在使用谷歌表格来保存共享项目的数据。使用 Google 的 Sheets API 我访问数据,在 python 中处理它,我试图在函数编写器中使用batchUpdate更新 Sheets 文件。

  • 如果我将此函数数据作为列表传递,它会按预期工作。
  • 如果我传递一个数据框(如我所愿),我会得到: TypeError: Object of type DataFrame is not JSON serializable
  • 如果我使用 .to_json(),我会得到这个:

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}}""。详细信息:“[{'@type':'type.googleapis.com/google.rpc.BadRequest','fieldViolations':[{'field':'data[0].values','description':'无效值在 'data[0].values' (type.googleapis.com/google.protobuf.ListValue), "{"0":{"0":1},"1":{"0":2}," 2":{"0":3},"3":{"0":4}}"'

任何指针将不胜感激。

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

# …
Run Code Online (Sandbox Code Playgroud)

python google-sheets pandas google-sheets-api

2
推荐指数
1
解决办法
3714
查看次数

标签 统计

google-sheets ×1

google-sheets-api ×1

pandas ×1

python ×1