Python 3.x &引用;“谷歌电子表格数据框架”;类型错误:';数据帧';对象是可变的,因此不能对其进行散列
试图在不使用gspread_pandas或df2gspread库的情况下将数据帧插入google电子表格。 因为在使用这些库时,用户必须将凭据json文件放入%appdata。 假设我在一个实例中有五个数据框,我想将这些数据框推送到五个不同的电子表格中。这些库无法做到这一点,所以下面是我尝试的代码,下面给出了这个错误Python 3.x &引用;“谷歌电子表格数据框架”;类型错误:';数据帧';对象是可变的,因此不能对其进行散列,python-3.x,google-sheets-api,Python 3.x,Google Sheets Api,试图在不使用gspread_pandas或df2gspread库的情况下将数据帧插入google电子表格。 因为在使用这些库时,用户必须将凭据json文件放入%appdata。 假设我在一个实例中有五个数据框,我想将这些数据框推送到五个不同的电子表格中。这些库无法做到这一点,所以下面是我尝试的代码,下面给出了这个错误 import pandas as pd from oauth2client.service_account import ServiceAccountCredentials imp
import pandas as pd
from oauth2client.service_account import ServiceAccountCredentials
import numpy as np
import gspread
from googleapiclient import discovery
data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'])
print(df)
scope = ["https://spreadsheets.google.com/feeds", 'https://www.googleapis.com/auth/spreadsheets',
"https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]
credentials = ServiceAccountCredentials.from_json_keyfile_name('cred.json', scope)
client = gspread.authorize(credentials)
sheet = client.open('SpreadsheetName').sheet1
sheets_service = discovery.build('sheets','v4',credentials=credentials)
sheets = sheets_service.spreadsheets()
spreadsheet_id = '************************'
def df_to_sheet(df):
df_columns = [np.array(df.columns)]
df_values = df.values.tolist()
df_to_sheet = np.concatenate((df_columns, df_values)).tolist()
return df_to_sheet
data = [
{
"title": "Unique URL",
"df": pd.DataFrame({})
}
]
update_body = {
"valueInputOption": "RAW",
"data": list(map(lambda d: {"range": d.get("title"), "values": df_to_sheet(d.get(df))}, data))
}
sheets.values().batchUpdate(spreadsheetId=spreadsheet_id, body=update_body).execute()
错误:
Name Age
0 Alex 10
1 Bob 12
2 Clarke 13
Traceback (most recent call last):
File "E:/Development/Selenium/slackdata/OLD Database/unique_url_finder_test.py", line 37, in <module>
"data": list(map(lambda d: {"range": d.get("title"), "values": df_to_sheet(d.get(df))}, data))
File "E:/Development/Selenium/slackdata/OLD Database/unique_url_finder_test.py", line 37, in <lambda>
"data": list(map(lambda d: {"range": d.get("title"), "values": df_to_sheet(d.get(df))}, data))
File "E:\Development\Selenium\venv\lib\site-packages\pandas\core\generic.py", line 1665, in __hash__
raise TypeError(
TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed
Process finished with exit code 1
姓名年龄
0亚历克斯10
1鲍勃12
2克拉克13
回溯(最近一次呼叫最后一次):
文件“E:/Development/Selenium/slackdata/OLD Database/unique\u url\u finder\u test.py”,第37行,在
“数据”:列表(映射(lambda d:{“范围”:d.get(“标题”),“值”:df_到_表(d.get(df))},数据))
文件“E:/Development/Selenium/slackdata/OLD Database/unique\u url\u finder\u test.py”,第37行,在
“数据”:列表(映射(lambda d:{“范围”:d.get(“标题”),“值”:df_到_表(d.get(df))},数据))
文件“E:\Development\Selenium\venv\lib\site packages\pandas\core\generic.py”,第1665行,在\uu散列中__
提高打字错误(
TypeError:“DataFrame”对象是可变的,因此无法对其进行哈希
进程已完成,退出代码为1
Hi there@MDFARHAN!我对你的问题有些怀疑。当你说“试图在不使用gspread_pandas或df2gspread库的情况下向google电子表格插入数据帧”时,你能解释一下为什么需要这样做吗?我想描述一下操作背后的原因,以便找到一种更简单的方法来使用@Jacques GuzelHeron这是一款管理软件。