Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 280
A.Rahman Mahmoud Avatar asked Aug 23 '20 11:08

A.Rahman Mahmoud


1 Answers

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']

like image 65
gojandrooo Avatar answered Oct 11 '22 17:10

gojandrooo