从Google API、Python3接收电子表格信息时出错

2024-04-18 10:23:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在尝试从Google电子表格API获取数据时遇到了问题。 这是完整的代码源代码:

# -*- coding: utf-8 -*-

import gspread
import gspread_dataframe as gd
from oauth2client.service_account import ServiceAccountCredentials




# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('C:/Users/yoong/Desktop/Prediction/GoogleSpreadsheetAI-07379c2b7c0d.json', scope)
client = gspread.authorize(creds)

# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
sheet = client.open("Korean Stock Info").sheet1

existing = gd.get_as_dataframe(sheet)
updated = existing.append(sheet)
gd.set_with_dataframe(sheet, updated)

print(sheet.cell(6, 2).value)

我得到一个错误说

TypeError: cannot concatenate object of type "<class 'gspread.models.Worksheet'>"; only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid

但是我从变量列表中得到了正确的数据帧。你知道吗?你知道吗


Tags: thenamefromimportclientapidataframeuse
1条回答
网友
1楼 · 发布于 2024-04-18 10:23:50

existing.append的参数必须是一个数据帧或一个序列,您试图传递一个gspread工作表对象。正式的正确答案是

updated = existing.append(gd.get_as_dataframe(sheet))

但除此之外,您的程序中似乎还有一个逻辑缺陷,因为updated将只是原始版本的两倍版本。也许你打算做些

updated = existing.append(gd.get_as_dataframe(another_sheet))

相关问题 更多 >