使用python将数据框上传到Google表格时出错?

时间:2019-05-01 07:04:13

标签: python pandas google-sheets google-sheets-api pygsheets

我正在将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')

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

2 个答案:

答案 0 :(得分:0)

Json无法理解numpy整数,因此需要先将数据框转换为字符串或整数,然后再上传回Google表格。

ds = ds.applymap(str)

答案 1 :(得分: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