Сonditional color formatting in Pandas( pandas 中的С条件颜色格式)
本文介绍了 pandas 中的С条件颜色格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
条件格式中的任务(我想是使用样式) 巨蟒, pandas
有一个有两列的盘子
应突出显示第二个表 条件:- 如果第一个列数超过第二个列数,则为绿色;
- 如果第一个列数等于第二个列数,则为黄色;
- 如果第一个列数小于第二个列数,则为红色。[
谢谢您的帮助!
推荐答案的想法是使用Styler.apply
创建新的按条件填充样式的DataFrame,用于按条件设置行DataFrame.mask
:
def highlight(x):
c1 = 'background-color: green'
c2 = 'background-color: yellow'
c3 = 'background-color: red'
m1 = x.iloc[:, 0] > x.iloc[:, 1]
m2 = x.iloc[:, 0] == x.iloc[:, 1]
df1 = pd.DataFrame(c3, index=x.index, columns=x.columns)
return df1.mask(m1, c1).mask(m2, c2)
df.style.apply(highlight, axis=None)
编辑:
如果只需要设置一列,请使用numpy.select
:
def highlight(x):
c1 = 'background-color: green'
c2 = 'background-color: yellow'
c3 = 'background-color: red'
c = ''
m1 = x.iloc[:, 0] > x.iloc[:, 1]
m2 = x.iloc[:, 0] == x.iloc[:, 1]
df1 = pd.DataFrame(c, index=x.index, columns=x.columns)
df1.iloc[:, 1] = np.select([m1, m2], [c1, c2], default=c3)
return df1
这篇关于 pandas 中的С条件颜色格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:pandas 中的С条件颜色格式


基础教程推荐
猜你喜欢
- 多索引数据帧删除每个组具有最大值的行 2022-09-22
- 当按下按钮时,将动态创建的按钮信息传递给函 2022-09-21
- 使用pyinstaller后,Python应用程序无法运行,但未显 2022-09-21
- IBM Watson SpechtoTextV1错误-Python 2022-09-22
- 使用Python访问已在运行的进程 2022-09-21
- Socket.recv为空,但结果显示在Maya中 2022-09-21
- 在VBA中调用python代码:权限错误 2022-09-22
- GridSearchCV.Best_Score不同于CROSS_VAL_Score(GridSearchCV. 2022-09-21
- 跟在带量词的前瞻后面有什么作用? 2022-09-22
- H5py:如何在HDF5组和数据集上使用key()循环 2022-09-21