6

I'm trying to color just a certain output, something like this

df['a'] = df['a'].fillna(df['b'].map(s)).style.applymap(lambda x: "background-color:yellow")

so I get the error

AttributeError: 'Series' object has no attribute 'style'

How can I make it right and color only the output that comes out of this?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
A.Rahman Mahmoud
  • 328
  • 1
  • 3
  • 17
  • This might help, https://stackoverflow.com/questions/41654949/pandas-style-function-to-highlight-specific-columns – abdul Aug 23 '20 at 11:34
  • Nope that's not what I'm looking for, this highlights whole columns which I don't need, I need to highlight the cells got printed by a certain code – A.Rahman Mahmoud Aug 23 '20 at 11:38
  • 1
    Please elaborate your question more..by giving a dummy `df` example. Also `style` function applies on `DataFrame` and you are applying on `Series`. – Abhilash Awasthi Aug 23 '20 at 11:51

1 Answers1

9

As someone called out, style formatter is for DataFrames and you are working with a Series. You can either preface the .style call with .to_frame() e.g.

df['a'] = df['a'].fillna(df['b'].map(s)).to_frame().style.applymap(lambda x: "background-color:yellow")

Or, less explicitly, to work with a DataFrame rather than Series wrap your column names in another set of brackets e.g. df[['a']] instead of df['a']

gojandrooo
  • 168
  • 2
  • 5