Issue
Here is the dataframe, what I want is to make the null cells 'grey' background color and 'yellow' background color contains the string 'BLANK'. AND only make actions on COLUMN 'B','C','D','E'.
df1 = pd.DataFrame({'A' : [np.nan, np.nan,np.nan,np.nan, np.nan, np.nan],
'B' : [np.nan, np.nan, np.nan, 'QAHQ052', np.nan,'NO AWD'],
'C' : [np.nan, '298561806', np.nan,'(BLANK)', np.nan, np.nan],
'D' : [np.nan, np.nan, '26748',np.nan, np.nan, np.nan],
'E' : ['WER', np.nan, np.nan,'QAC9536, QCD03532, QA39535, (BLANK)', np.nan, 'OGA']})
Solution
You can use pandas.io.formats.style.Styler.apply
:
def highligth_nan(ser):
highlight = 'background-color: gray'
default = ''
return [highlight if pd.isna(e) else default for e in ser]
def highligth_blank(ser):
highlight = 'background-color: yellow'
default = ''
return [highlight if 'BLANK' in str(e) else default for e in ser]
cols = ['B', 'C', 'D', 'E']
(
df1
.style
.apply(highligth_nan, axis=0, subset=cols)
.apply(highligth_blank, axis=0, subset=cols)
)
# Output :
Answered By - M92_
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.