[英]how to go to particular column in csv file and print the whole row if in column value is less than particular value
[英]How to use multiple background colors for different string value in a particular row of csv file
我有包含多列的 CSV 文件,我正在第 5 列中搜索一些字符串并突出显示不同字符串的行。 到目前为止,我只能使用两种颜色,但我想使用更多的 colors 来获得更多的字符串。 示例
x['4']="abc" --->I want to use yellow color
x['4']="xyz" --->I want to use grey color
x['4']="pqr" --->I want to use green color
对于我正在使用的两种颜色
n = len(df.columns)
df.style.apply(lambda x: ["background-color: red"]*n if x['4']== 'ERROR' else ["background-color: white"]*n, axis = 1)
我们应该对多种颜色进行哪些更改
为后台 colors 按值创建字典,并通过dict.get
传递给Styler.applymap
进行映射,如果没有匹配则返回空值:
d = {'abc':'background-color:yellow',
'xyz':'background-color:grey',
'pqr':'background-color:green'}
df.style.applymap(lambda x: d.get(x,''), subset=['4'])
Styler.apply
和axis=1
的另一个想法,用于映射使用Series.map
并用Series.fillna
替换缺失值:
df.style.apply(lambda x: x.map(d).fillna(''), subset=['4'], axis=1)
样本数据:
df = pd.DataFrame({'4':['abc','xyz','pqr','temp'],
'5':range(4)})
在 jupyter notebook 中测试:
编辑:
对于着色行使用自定义 function:
def hightlight(x):
d = {'abc':'background-color:yellow',
'xyz':'background-color:grey',
'pqr':'background-color:green'}
#DataFrame with same index and columns names as original filled repeated strings
out = np.repeat(x['4'].map(d).fillna('').to_numpy(), len(x.columns)).reshape(x.shape)
return pd.DataFrame(out, index=x.index, columns=x.columns)
df.style.apply(hightlight, axis=None)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.