使用python将数据帧上载到google工作表时出错?

2024-04-18 19:13:56 发布

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

我获取一个google表单作为pandas数据帧,然后在df中添加一列,然后将其上传回来。我面临这个错误:

Object of type 'int64' is not JSON serializable

代码如下:

#reading and writing on google sheets

gc = pygsheets.authorize(service_file="/Projects/big_query_funnel_v1/input/AppFunnelgs-e1968da.json")

sheet=gc.open('Daily Drop Offs Summary')
wks = sheet.worksheet_by_title('New_Funnel')

#converting to pandas df
ds = wks.get_as_df()

#making a new column
ds.insert(loc=1, column=str(dates), value=edf.vals)

print(ds.head())

wks.set_dataframe(ds, 'A1')

我得到的错误在最后一行代码,即当上传回测向。你知道吗


Tags: of数据代码表单pandasdfobjecttype
2条回答

以下工作(不修改逻辑):

edf = pd.DataFrame({"vals":[1,2,3,4,5]}, dtype="int64")
dates = "column_name"



#reading and writing on google sheets

gc = pygsheets.authorize(service_file="/Projects/big_query_funnel_v1/input/AppFunnelgs-e1968da.json")

sheet=gc.open('Daily Drop Offs Summary')
wks = sheet.worksheet_by_title('New_Funnel')

#converting to pandas df
ds = wks.get_as_df()

#making a new column
ds.insert(loc=1, column=str(dates), value=edf.vals)

print(ds.head())

wks.set_dataframe(ds, 'A1')

软件包版本:

pygsheets==2.0.1
pandas==0.23.4

Json不理解numpy整数,所以在上传回googlesheets之前,需要将数据帧转换为string或int。你知道吗

ds = ds.applymap(str)

相关问题 更多 >