[英]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.