小编Rod*_*ani的帖子

使用 df2gsheets 将 pandas DF 从 python 导入 google 表格时出现数据格式问题

我正在使用 df2gspread 将某个 pandas df 导入到谷歌表格中。该过程运行没有任何问题,但我想在 Gsheets 中操作的数字信息作为文本导入。当我对以文本形式存储的数据使用基本数学运算时,它可以工作,但是当我尝试使用 Sheets 函数(例如求和、平均值和几乎任何其他函数)时,返回的值始终为零。另外,如果我尝试在 gsheet 本身内手动将文本转换为数字,它不会产生任何效果。

代码如下:

import pandas as pd
import gspread as gs
from df2gspread import df2gspread as d2g

result = tera.execute_response("select * from table_drive")
df = pd.DataFrame(result)

scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name(
    'json_gsheets.json', scope)
gc = gs.authorize(credentials)

spreadsheet_key = 'insert_wks_key_here'
wks = 'import'
d2g.upload(df, spreadsheet_key, wks, credentials=credentials, row_names=False,start_cell = 'B3')
Run Code Online (Sandbox Code Playgroud)

这会正确插入数据,但所有内容都以文本形式不可撤销地存在。

有人可以帮忙吗?

提前致谢!

python google-sheets pandas gspread google-sheets-api

4
推荐指数
1
解决办法
1401
查看次数