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 ==)
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.
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']
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