[英]Pygsheets: How to highlight cells in a specific column based on a condition
I have a pandas data frame with three columns, and I want to highlight the cells in a specific column that meet a certain condition using pygsheets.我有一个包含三列的 Pandas 数据框,我想使用 pygsheets 突出显示特定列中满足特定条件的单元格。 How can I do that?
我怎样才能做到这一点?
A B C
some_text 65 some_text
def color_my_cell(var):
for i in wks.range('B3:B30'):
if var < 70:
wks.cell( NOT SURE what to do here).color = (1.0,0,1.0,1.0)
df['B'] = df['B'].apply(color_my_cell)
So, for any cells in B < 70, highlight cells blue.因此,对于 B < 70 中的任何单元格,将单元格突出显示为蓝色。
you cant apply formats in df directly.您不能直接在 df 中应用格式。 you can do this by coloring cells by acessing each cell and setting color.
您可以通过访问每个单元格并设置颜色来为单元格着色。 but i think in your case whats more appropriate is conditional formatting.
但我认为在你的情况下更合适的是条件格式。 something like shown below.
如下所示。 below code might not be exatly what you require refer to here and here
下面的代码可能不是您所需要的,请参阅此处和此处
request = {"AddConditionalFormatRuleRequest": {
"ranges":[GridRange(worksheet=wks, start='B3', end='B30').to_json()],
"booleanRule": {
"condition":{"type::'NUMBER_GREATER', 'values': 70},
"format":{"backgroundColor":{"color": {"green": 0.8,},}}}},
"index":0}
ssheet.custom_request(request, "*")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.