Python 上传熊猫数据框到谷歌电子表格
我遵循这些步骤,但无法将熊猫数据框上传到谷歌表单 首先,我尝试了以下代码:Python 上传熊猫数据框到谷歌电子表格,python,pandas,google-sheets,google-sheets-api,gspread,Python,Pandas,Google Sheets,Google Sheets Api,Gspread,我遵循这些步骤,但无法将熊猫数据框上传到谷歌表单 首先,我尝试了以下代码: import gspread from google.oauth2.service_account import Credentials scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive'] credentials = Credentials.from_service_ac
import gspread
from google.oauth2.service_account import Credentials
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
credentials = Credentials.from_service_account_file('my_json_file_name.json', scopes=scope)
gc = gspread.authorize(credentials)
spreadsheet_key = '1FNMkcPz3aLCaWIbrC51lgJyuDFhe2KEixTX1lsdUjOY'
wks_name = 'Sheet1'
d2g.upload(df_qrt, spreadsheet_key, wks_name, credentials=credentials, row_names=True)
上面的代码返回如下错误消息:AttributeError:module'df2gspread'没有属性'upload'
,这没有意义,因为df2spread确实有一个名为upload的函数
其次,我试图通过输入列名将数据附加到我在google工作表上人工创建的数据框中。这也不起作用,也没有提供任何结果
import gspread_dataframe as gd
ws = gc.open("name_of_file").worksheet("Sheet1")
existing = gd.get_as_dataframe(ws)
updated = existing.append(df_qrt)
gd.set_with_dataframe(ws, updated)
任何帮助都将不胜感激,谢谢 您没有正确导入包
updated = existing.append(df_qrt)
gd.set_with_dataframe(ws, updated)
就这么做吧
from df2gspread import df2gspread as d2g
使用将工作表转换为数据帧时
existing = gd.get_as_dataframe(ws)
表中的所有空白列和行现在是数据框的一部分,值为Na,因此当您试图用另一个数据框追加它时,由于列不匹配,因此不会追加。 相反,请尝试将工作表转换为数据帧
existing = pd.DataFrame(ws.get_all_records())
当您在Google Sheets中导出数据框时,数据框的索引存储在第一列中(在我的例子中发生过,不能确定)。
如果第一列是索引,则可以使用
existing.drop([''],axis=1,inplace=True)
这样就可以正常工作了
updated = existing.append(df_qrt)
gd.set_with_dataframe(ws, updated)
您没有包含
df2gspread
的导入语句,但您是否说import df2gspread as d2g
而不是从df2gspread导入df2gspread as d2g
?这将解释属性错误。