如何在 gspread-formatting 中应用列格式
我正在尝试在 gspread 的帮助下应用列格式,同时将数据框导出到谷歌表。我正在使用 format_with_dataframe 方法,但我不确定如何传递列名和列名;作为 column_formats 属性一部分的数据类型。
我在下面分享了我的代码。
from gspread_formatting import *
from gspread_formatting.dataframe import format_with_dataframe, BasicFormatter
from gspread_dataframe import get_as_dataframe, set_with_dataframe
from google.colab import auth
auth.authenticate_user()
import gspread
from oauth2client.client import GoogleCredentials
gc = gspread.authorize(GoogleCredentials.get_application_default())
import pandas as pd
data = [['tom', 10.1123, 0.23], ['nick', 15.3445, 0.3224], ['juli', 14.12, 0.023], ['robert', 9.0809, 0.722]]
df = pd.DataFrame(data, columns = ['Name', 'S-Mark', '%-Value'])
sh = gc.create("Test")
worksheet = gc.open("Test").worksheet("Sheet1")
set_with_dataframe(worksheet, df)
formatter = BasicFormatter(
header_background_color=Color(0,0,0),
header_text_color=Color(1,1,1),
column_formats= {'S-Mark':'{:,.0f}'.format,'%-Value':'{:,.2f}'.format }
)
format_with_dataframe(worksheet, df, formatter, include_index=False, include_column_header=True)
我也分享示例输出
I am trying to apply column formatting while exporting the dataframe to google sheet with the help of gspread. I am using format_with_dataframe method but I am unsure on how to pass column-name & data types as part of column_formats attribute.
I have shared my code below.
from gspread_formatting import *
from gspread_formatting.dataframe import format_with_dataframe, BasicFormatter
from gspread_dataframe import get_as_dataframe, set_with_dataframe
from google.colab import auth
auth.authenticate_user()
import gspread
from oauth2client.client import GoogleCredentials
gc = gspread.authorize(GoogleCredentials.get_application_default())
import pandas as pd
data = [['tom', 10.1123, 0.23], ['nick', 15.3445, 0.3224], ['juli', 14.12, 0.023], ['robert', 9.0809, 0.722]]
df = pd.DataFrame(data, columns = ['Name', 'S-Mark', '%-Value'])
sh = gc.create("Test")
worksheet = gc.open("Test").worksheet("Sheet1")
set_with_dataframe(worksheet, df)
formatter = BasicFormatter(
header_background_color=Color(0,0,0),
header_text_color=Color(1,1,1),
column_formats= {'S-Mark':'{:,.0f}'.format,'%-Value':'{:,.2f}'.format }
)
format_with_dataframe(worksheet, df, formatter, include_index=False, include_column_header=True)
I am sharing sample output as well
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论