[英]Pandas change cell color
如果單元格包含字符串列表中的字符串,我正在嘗試更改單元格顏色:
這允許我在匹配時更改顏色,但它不會通過列表中的每個項目出現在 go 中,它只進行第一個匹配(我認為這是因為 ==)
def color_negative_red(val):
for technique in techniques:
color = 'green' if val == technique else 'black'
return 'color: %s' % color
我正在嘗試以下方法,但這似乎並沒有改變任何顏色:
def color_negative_blue(val):
for technique in techniques:
color = 'blue' if technique in val else 'black'
return 'color: %s' % color
我將這些函數稱為如下:
s = df1.style.applymap(color_negative_blue)
我希望能夠對項目列表進行 go ,如果列表中的項目存在,則更改單元格中該文本的顏色。
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']
當其內容與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}'
上面的代碼允許我執行搜索並更改它匹配的單元格的顏色
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.