简体   繁体   English

Pandas 改变电池颜色

[英]Pandas change cell color

I am trying to change the cell colour if the cell contains a string from a list of strings:如果单元格包含字符串列表中的字符串,我正在尝试更改单元格颜色:

This allows me to change the colour if there is a match but it doesn't appear to go through every item in the list it only does the first match (i think this is because of the ==)这允许我在匹配时更改颜色,但它不会通过列表中的每个项目出现在 go 中,它只进行第一个匹配(我认为这是因为 ==)

def color_negative_red(val):
    for technique in techniques:
        color = 'green' if val == technique else 'black'
        return 'color: %s' % color

I am trying the following but that doesn't appear to be changing any colours:我正在尝试以下方法,但这似乎并没有改变任何颜色:

def color_negative_blue(val):
    for technique in techniques:
        color = 'blue' if technique in val else 'black'
        return 'color: %s' % color

I call these functions as followed:我将这些函数称为如下:

s = df1.style.applymap(color_negative_blue)

I want to be able to go over a list of items and if the item from the list exists then change the colour of that text in the cell.我希望能够对项目列表进行 go ,如果列表中的项目存在,则更改单元格中该文本的颜色。

DataFrame: DataFrame:

Column1                Column2              Column3          Column4

Acquire               Infrastructure        Botnet      Drive-by Compromise 
DNS Server                                               Exploit Public-Facing Application  
Domains                                                 External Remote Services    
Server                                                  Hardware Additions  
Virtual Private Server                    Phishing      Spearphishing Attachment
Web Services                              Spearphishing Link
Compromise Accounts Email Accounts                      Spearphishing via Service
Social Media Accounts                                   Replication Through Removable Media 


techniques = ['Server','Web Services', 'Phishing']

Replace any cell when its content matches techniques :当其内容与techniques匹配时替换任何单元格:

def my_func_blue(val):
   if val in techniques:
      return "color: blue"
   else:
      return "color: black"

df.style.applymap(my_func_blue).render()
def my_func_blue(val):
    if val in techniques:
        color = 'green'
        return f'background-color: {color}'
    else:
        color = 'red'
        return f'background-color: {color}'

the above code allows me to carry out the searches and change the color of the cell that it has matched on上面的代码允许我执行搜索并更改它匹配的单元格的颜色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM