当尝试使用 googledrive api 将旋转数据框上传到 googlesheet 时,系统提示我出现以下错误:
在检查 dtypes 后,我发现我的索引值是对象,并且我也将列值转换为对象,但我仍然收到错误。
import gameData_connection
import pandas as pd
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from df2gspread import df2gspread as d2g
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials_files.json', scope)
gc = gspread.authorize(credentials)
spreadsheet_key = 'my_spreadsheet_key'
wks_name = 'My File Name'
connection = gameData_connection.getConnection()
print('Connect Succesful')
try:
with connection.cursor() as cursor:
sql1 = This is my whole sql query
cursor.execute(sql1)
df1 = pd.read_sql_query(sql1, connection)
print('cursor.description:', cursor.description)
print()
for row in cursor:
print(row)
finally:
connection.close()
table1 = pd.pivot_table(df1, index = ["game", "platform"],
values = ["new_users", "dau", "day1_retention", "day6_retention", "revenue", "arpdau", "arppu", "pau"],
columns = ["year_time", "month_time"])
d2g.upload(table1, spreadsheet_key, wks_name, credentials=credentials, row_names=True)
print(table1)
Out[21]:
time_join int64
year_time int64
month_time int64
game object
platform object
new_users float64
dau float64
day1_retention float64
day6_retention float64
revenue float64
arpdau float64
arppu float64
pau float64
dtype: object
我不明白我在这里做错了什么。如果我运行
d2g.upload(df1, spreadsheet_key, wks_name, credentials=credentials, row_names=True)
,它就会起作用,所以表 1 内一定发生了问题。
现在请注意,如果编写相同的 table1 来执行 excel,它就可以工作。
预先感谢您的帮助
我在使用 df2gspread 库实现时遇到了类似的问题。在这里提供我的解决方案,因为这个问题没有得到解答(而且很老了!)
df2gspread 库的编写方式是尝试将数据帧的列和索引转换为字符串。 Pandas 不支持 MultiIndex 类型的转换,并会生成您看到的 TypeError。 问题已发布在此处的存储库中,但尚未解决。
解决方案是使用 gspread_pandas 库,该库具有支持直接包括 MultiIndex 行和列的数据帧的实现,如示例中的数据透视表。
import gameData_connection
import pandas as pd
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from gspread_pandas import Spread # NEW LINE
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials_files.json', scope)
gc = gspread.authorize(credentials)
spreadsheet_key = 'my_spreadsheet_key'
wks_name = 'My File Name'
# NEW LINE
sh = Spread(spred=spreadsheet_key, creds=credentials)
connection = gameData_connection.getConnection()
print('Connect Successful')
try:
with connection.cursor() as cursor:
sql1 = This is my whole sql query
cursor.execute(sql1)
df1 = pd.read_sql_query(sql1, connection)
print('cursor.description:', cursor.description)
print()
for row in cursor:
print(row)
finally:
connection.close()
table1 = pd.pivot_table(df1, index = ["game", "platform"],
values = ["new_users", "dau", "day1_retention", "day6_retention", "revenue", "arpdau", "arppu", "pau"],
columns = ["year_time", "month_time"])
# NEW LINE
sh.df_to_sheets(df=table1, # Your dataframe
sheet=wks_name, # Target sheet name
start='A1', # First cell to place the data in
index=True, # Show the index
headers=True, # Show the columns
merge_headers=True, # Merge the cells of multiindex headers
merge_index=True, # Merge the rows of the muliindex index
freeze_headers=True, # Freeze the header rows
)