通常只是使用带有自定义定义的pandas styler来根据某些条件为数据框的值着色。它很好用,使用了applymap函数。但是,现在我有一个问题,我想运行另一个定义并为满足特定条件的列标签上色(我的列标签只是一堆介于1到100之间的整数)。我在列标签中有一个要被涂成红色的整数列表。
关于如何将样式功能扩展到实际标签而不是单元格的任何想法?
我只尝试使用applymap或apply将函数应用于整个数据框或特定列。
"""
Takes a scalar and returns a string with
the css property colour depending on which amino acid set it belongs to """
Green = ["S","T","N","Q"]
Blue = ["K","R","H"]
Orange = ["F","W","Y"]
Pink = ["I","L","V","A","M"]
Red = ["D","E"]
Purple = ["G","P"]
Yellow = ["C"]
if val in Green:
color = 'green'
#return ['background-color: %s' if v else '' for val in is_max]
return 'background-color: %s' % color
if val in Blue:
color = 'blue'
return 'background-color: %s' % color
if val in Orange:
color = 'orange'
return 'background-color: %s' % color
if val in Pink:
color = 'pink'
return 'background-color: %s' % color
if val in Red:
color = 'red'
return 'background-color: %s' % color
if val in Purple:
color = 'purple'
return 'background-color: %s' % color
if val in Yellow:
color = 'Yellow'
return 'background-color: %s' % color
else:
color = "white"
return 'background-color: %s' % color
styled = c.style.applymap(amino_acid_colouring)
styled.to_excel('styled.xlsx', engine='openpyxl')```