1

我正在获取一个谷歌表作为熊猫数据框,然后在 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')

我在最后一行代码中遇到错误,即在上传回 df 时。

4

2 回答 2

0

Json 不理解 numpy 整数,因此需要将数据帧转换为字符串或整数,然后再上传回谷歌表格。

ds = ds.applymap(str)
于 2019-05-01T07:31:02.813 回答
0

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

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
于 2019-05-01T07:49:02.870 回答