列中的条件格式单元格基于它在另一列中的对应值

时间:2017-10-24 17:20:20

标签: python excel xlsxwriter

我已经从数据框创建了一个excel文件,如下所示:

In [215]: import pandas as pd

In [216]: df = pd.DataFrame({"Name": ["A", "B", "C"], "Status": ['y', 'n', 'yy']})

In [217]: df
Out[217]:
  Name  Status
0    A       y
1    B       n
2    C      yy

如何根据bg_color的值为“名称”设置Status?我尝试了几个选项没有成功:

format1 = workbook.add_format({"bg_color": "#669731"})
format2 = workbook.add_format({"bg_color": "#FFFA22"})
format3 = workbook.add_format({"bg_color": "#A43829"})

选项1

worksheet.conditional_format("A2",
                             {"type": "formula",
                              "criteria": "=ISNUMBER(SEARCH('y', B2))",
                              "format": format1
                             }
)

选项2

worksheet.conditional_format("A2",
                             {"type": "formula",
                              "criteria": "=$B$2='y'",
                              "format": format1
                             }
)

这些都没有给出预期的结果,当我打开文件时,出现以下消息时出现错误: .xlsx中不可读的内容
如果我能以某种方式设置这样做而不迭代数据帧的值,那将是很好的。

1 个答案:

答案 0 :(得分:4)

Excel似乎不喜欢字符串上条件格式的单引号。如果内部有双引号,即

,则有效
"criteria": '=($B$2="y")' 

"criteria": "=($B$2='y')"

我已经在下面提供了一个完整的可重现的示例,其中包含解决方案的屏幕截图。

import pandas as pd

df = pd.DataFrame({"Name": ["A", "B", "C"], "Status": ['y', 'n', 'yy']})

writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')

df.to_excel(writer, sheet_name='Sheet1', index=False)


workbook  = writer.book
worksheet = writer.sheets['Sheet1']

format1 = workbook.add_format({"bg_color": "#669731"})

worksheet.conditional_format("A2",
                             {"type": "formula",
                              "criteria": '=($B$2="y")',
                              "format": format1
                             }
)

workbook.close()

screenshot conditional formatting

如果要为列中的1000个单元格设置此条件格式,则可以使用条件格式的代码。

worksheet.conditional_format("A2:A1001",
                             {"type": "formula",
                              "criteria": '=(B2:B1001="y")',
                              "format": format1
                             }
)

另一方面,如果你想在一个范围内设置多个条件,我认为这是可能的唯一方法是使用for循环,用匹配条件的格式写每个单元格。我已经提供了下面的例子以及它的预期输出。请注意,如果它满足三个条件中的任何一个条件,它会覆盖已放入单元格中的内容,这有点作弊。

import pandas as pd

df = pd.DataFrame({"Name": ["A", "B", "C"], "Status": ['y', 'n', 'yy']}) 
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter') 
df.to_excel(writer, sheet_name='Sheet1', index=False) 

workbook  = writer.book 
worksheet = writer.sheets['Sheet1'] 

format1 = workbook.add_format({"bg_color": "#669731"})
format2 = workbook.add_format({"bg_color": "#FFFA22"})
format3 = workbook.add_format({"bg_color": "#A43829"})

for i in range (0, len(df)):
    if df['Status'].ix[i] == "y":
        worksheet.write(i+1, 0, df['Name'].ix[i], format1)
    elif df['Status'].ix[i] == "n":
        worksheet.write(i+1, 0, df['Name'].ix[i], format2)
    elif df['Status'].ix[i] == "yy":
        worksheet.write(i+1, 0, df['Name'].ix[i], format3)


workbook.close()  

enter image description here