2024-09-30 00:31:57 发布
网友
如果列的style.bar.color属性是基于某种条件计算的,我如何呈现Pandas df?在
style.bar.color
示例:
df.style.bar(subset=['before', 'after'], color='#ff781c', vmin=0.0, vmax=1.0)
我希望其中一列(df['before'])保持不变的颜色,另一列(df['after'])计算如下:
df['before']
df['after']
一种方法是使用pd.IndexSlice为df.style.bar创建子集:
pd.IndexSlice
df.style.bar
i_pos = pd.IndexSlice[df.loc[(df['after']>df['before'])].index, 'after'] i_neg = pd.IndexSlice[df.loc[~(df['after']>df['before'])].index, 'after'] df.style.bar(subset=['before'], color='#ff781c', vmin=0.0, vmax=1.0)\ .bar(subset=i_pos, color='green', vmin=0.0, vmax=1.0)\ .bar(subset=i_neg, color='red', vmin=0.0, vmax=1.0)
输出:
一种方法是使用
pd.IndexSlice
为df.style.bar
创建子集:输出:
相关问题 更多 >
编程相关推荐