任务:我有一个数据框,我想将该数据附加到 Google 表格中。该工作表具有与 DF 完全相同的列,并且具有我不想覆盖的现有数据,但只需使用 DF 中的值在工作表末尾添加行。
到目前为止尝试过:
import json
df2.to_json(orient = 'columns')
values = df2.to_json(orient = 'columns')
wks.append_row (values, value_input_option='USER_ENTERED')
Run Code Online (Sandbox Code Playgroud)
结果:APIError:{“错误”:{“代码”:400,“消息”:“'data.values [0]'处的值无效(type.googleapis.com/google.protobuf.ListValue),
!pip install --upgrade google-api-python-client
from pprint import pprint
from googleapiclient import discovery
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/spreadsheets']
credentials = ServiceAccountCredentials.from_json_keyfile_name('NAME.json', scope)
gc = gspread.authorize(credentials)
wks = gc.open("SHEETNAME").sheet1
service = discovery.build(wks,'v1', credentials)
spreadsheet_id = 'MYID'
value_input_option = INSERT_ROWS
values = df2.to_json(orient = 'columns')
insert_data_option …Run Code Online (Sandbox Code Playgroud)