简体   繁体   English

Pandas Styler 子集列按值

[英]Pandas Styler Subset column by values

I'm using the following to color the cells in a dataframe:我正在使用以下内容为 dataframe 中的单元格着色:

import seaborn as sns

cm1 = sns.diverging_palette(h_pos=130, h_neg=10, s=99, l=55, n=99, as_cmap=True)

df_s = (df.style
    .background_gradient(cmap=cm1, subset=['col1']))

This successfully applies the background gradient to the values in col1这成功地将背景渐变应用于col1中的值

However, I'd like to something like the following:但是,我想要以下内容:

df_s = (df.style
    .background_gradient(cmap=cm1, subset=['col1'] < x))

Which does not work哪个不起作用

The idea is to only apply the gradient to values in col1 which are less than x , and display the full dataframe where col1 >= x is un-colored.这个想法是仅将渐变应用于col1中小于x的值,并显示完整的 dataframe ,其中col1 >= x是未着色的。

Seems like there should be an easy way to do this but I can't seem to get the argument into the right format for subset .似乎应该有一种简单的方法来做到这一点,但我似乎无法将参数转换为subset的正确格式。

Thanks in advance for the help!在此先感谢您的帮助!

You need to use pd.IndexSlice :您需要使用pd.IndexSlice

import seaborn as sns

cm1 = sns.diverging_palette(h_pos=130, h_neg=10, s=99, l=55, n=99, as_cmap=True)
np.random.seed(123)
df = pd.DataFrame(np.random.randint(0,100,(5,5)), columns=[*'ABCDE'])
df.style.background_gradient(cmap=cm1, subset=pd.IndexSlice[df['C']<50, 'C'])

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 熊猫多索引列样式器 - pandas multiindex column styler Pandas:为子集中的每一列寻找最大值 - Pandas : finding maximum values for each column in a subset Pandas - 检查列中的设置值是否是另一列中设置值的子集 - Pandas - check if set values in column are a subset of set values in another column 应用样式器功能后如何删除列? pandas.io.formats.style.Styler.hide_columns - 不工作 - How to delete column after applying styler function? pandas.io.formats.style.Styler.hide_columns - not work 在同一列上使用 Pandas Styler 时合并条形图并突出显示 - Combine bar chart and highlight when using Pandas Styler on the same column Pandas,基于列值的唯一子集追加列 - Pandas, append column based on unique subset of column values "<i>pandas Styler.<\/i>熊猫造型器。<\/b> <i>How to ignore the index column from the rendered HTML<\/i>如何从呈现的 HTML 中忽略索引列<\/b>" - pandas Styler. How to ignore the index column from the rendered HTML 根据每个单元格的索引和列向 pandas Styler 表添加超链接 - Add hyperlinks to pandas Styler table depending on index and column of each cell 创建熊猫列的Pythonic / fast方法:列值的子集和 - Pythonic/fast method to create pandas column: subset sum of column values 子集参数在 pandas.io.formats.style.Styler.format 中有什么作用? - What does the subset argument do in pandas.io.formats.style.Styler.format?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM