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?
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With