【问题标题】:Error while uploading dataframe to google sheets using python?使用python将数据框上传到谷歌表格时出错?
【发布时间】:2019-05-01 07:04:13
【问题描述】:

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

【问题讨论】:

  • 看起来你需要做一些投射
  • 实际上我只是累了,它甚至可以使用 'int64' - 它在内部进行一些转换 - 尝试更新所有包!

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


【解决方案1】:

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

ds = ds.applymap(str)

【讨论】:

    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-20
      相关资源
      最近更新 更多