熊猫仅适用于某些行

时间:2021-07-23 07:42:47

标签: python pandas

请指教,如何修改以下代码以仅适用于 p-value 的索引?现在它还用我不想要的 Mean of cases with data 突出显示索引。谢谢!!

        def p_value_bold(val):
            bold = 'bold' if (val <= 0.05) & (val > 0) else ''
            return 'font-weight: %s' % bold
        
        def negative_yellow(val):
            color = 'yellow' if (val <= 0.05) & (val > 0) else ''
            return 'background-color:' + color 
        
    return return_df.style.applymap(p_value_bold).applymap(negative_yellow)

enter image description here

1 个答案:

答案 0 :(得分:2)

如果您能提供文本形式的测试数据集会更好。

尽管如此,你能不能试试:

idx = pd.IndexSlice[return_df.index.get_level_values(level=1)=='p_value', :]
return_df.style.applymap(p_value_bold, subset=idx).applymap(negative_yellow, subset=idx)