使用 gspread API 对 Google 表格中的数字进行四舍五入

Jon*_*nis 4 python google-sheets pandas gspread

我正在使用 gspread 将 pandas 数据框写入 google 表格:

from gspread_formatting import *
import gspread
from df2gspread import df2gspread as d2g
import pandas as pd

d2g.upload(data, sheet.id, 'test_name', clean=True, credentials=creds, col_names=True, row_names=False)
Run Code Online (Sandbox Code Playgroud)

虽然 pandas 数据框四舍五入到2小数点,但谷歌表格中的值有时不止于此。

df

a               b
100.56          600.79
Run Code Online (Sandbox Code Playgroud)

谷歌表格中的结果:

aa                b
100.5616          600.79
Run Code Online (Sandbox Code Playgroud)

我找不到任何信息如何使用 python gspread API 对值进行舍入。

alp*_*ğlu 5

如果您获得工作表元素,您可以使用格式来实现您想要的。

sh = gc.open("sheet_name")
worksheet = sh.get_worksheet(0) # your sheet number
worksheet.format('A', {'numberFormat': {'type' : 'NUMBER', 'pattern': '0.0#'}})
Run Code Online (Sandbox Code Playgroud)