Python 如果数字等于,则为彩色背景单元格
我目前可以使用以下代码更改单元格的背景:Python 如果数字等于,则为彩色背景单元格,python,pandas,colors,background,background-color,Python,Pandas,Colors,Background,Background Color,我目前可以使用以下代码更改单元格的背景: def my_func_blue(val): if val in techniques: color = 'green' return f'background-color: {color}' elif val in Fulltechniques: color = 'red' return f'background-color: {color}' s = df1.style.
def my_func_blue(val):
if val in techniques:
color = 'green'
return f'background-color: {color}'
elif val in Fulltechniques:
color = 'red'
return f'background-color: {color}'
s = df1.style.applymap(my_func_blue)
s
我希望能够将另一个IF语句添加到
"if val in techniques"
因此,如果出现次数大于1,则应用颜色:
DF:
因此,如果逻辑更复杂,则只有包含云帐户的单元格的背景颜色为绿色如果可能存在多个条件,例如,此处的
m1
与m2
用于创建样式的数据框,如有必要,创建excel文件:
techniques = ['Cloud Account','Account Manipulation']
Fulltechniques = ['Sudo and Sudo Caching']
def my_func_blue(x):
c1 = 'background-color: green'
c2 = 'background-color: red'
c = ''
m1 = x.Technique_Name.str.contains('|'.join(techniques))
m2 = x.Technique_Name.str.contains('|'.join(Fulltechniques))
m3 = x.Occurance.gt(1)
df1 = pd.DataFrame(c, index=x.index, columns=x.columns)
df1 = df1.mask(m1 & m3, c1).mask(m2, c2)
return df1
(df.style.apply(my_func_blue,axis=None)
.to_excel('styled.xlsx', engine='openpyxl', index=False))
如果逻辑更复杂,则可能会链接多个条件,例如,此处使用
m1
和m2
创建样式的数据框架,必要时创建excel文件:
techniques = ['Cloud Account','Account Manipulation']
Fulltechniques = ['Sudo and Sudo Caching']
def my_func_blue(x):
c1 = 'background-color: green'
c2 = 'background-color: red'
c = ''
m1 = x.Technique_Name.str.contains('|'.join(techniques))
m2 = x.Technique_Name.str.contains('|'.join(Fulltechniques))
m3 = x.Occurance.gt(1)
df1 = pd.DataFrame(c, index=x.index, columns=x.columns)
df1 = df1.mask(m1 & m3, c1).mask(m2, c2)
return df1
(df.style.apply(my_func_blue,axis=None)
.to_excel('styled.xlsx', engine='openpyxl', index=False))
我已经为我的变量更改了它,但是在第一场比赛之后它似乎停止了,我需要它来完成它。在我上面的列表中,只有“Sudo和Sudo缓存”匹配为红色all@TiiTcHY-你现在能查一下吗?我更改了
isin
以匹配由|
连接的列表的所有值,对于regex或
我已更改了变量的值,但在第一次匹配之后,它似乎停止了,我需要它来完成所有匹配。在我上面的列表中,只有“Sudo和Sudo缓存”匹配为红色all@TiiTcHY-你现在能查一下吗?对于正则表达式或