I have a dataframe as below.. I want to highlight the cells if the value contains delimiter "--" I tried below code. but it throws me error :
| a | b | c | d |
|---|---|---|---|
| 1--2 | 3 | 4 | None |
| 2 | 34--32 | 12 | None |
| 23 | 123--12 | 123--11 | None |
| 12 | 11--111 | 11--333 | None |
def color_red(val):
color= 'red' if '|' in val else 'black'
return 'color: %s' % color
s = df.style.applymap(color_red)
st = s.render()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Python39\lib\site-packages\pandas\core\frame.py", line 7842, in infer
return lib.map_infer(x.astype(object)._values, func, ignore_na=ignore_na)
File "pandas\_libs\lib.pyx", line 2467, in pandas._libs.lib.map_infer
File "<stdin>", line 2, in color_red
TypeError: argument of type 'NoneType' is not iterable
You can try replace None to empty string before apply styles:
If need in ouput
NaNs is possible convert them to strings if checkinstatement: