Applying style on a dataframe returns a Styler object, not a DataFrame. You cannot apply further style operations on that.
What you can do is to apply all your styling operations with a single apply/applymap.
If that is too complex, a not too nice hack is to create new columns to make all your styling possible and then hide these columns with the style operation.
Answer from nocibambi on Stack OverflowApplying style on a dataframe returns a Styler object, not a DataFrame. You cannot apply further style operations on that.
What you can do is to apply all your styling operations with a single apply/applymap.
If that is too complex, a not too nice hack is to create new columns to make all your styling possible and then hide these columns with the style operation.
f = lambda v: 'background-color: %s' % 'green' if v=='col' else ''
df = df.style.applymap(f, subset=['a'])
if you use single style means above code will work fine. If you using multiple style means remove that style.
def format_color_groups(values):
if values =='Y':
return 'color:red;border-collapse: collapse; border: 1px solid black;'
elif values == 'N':
return 'color:green;border-collapse: collapse; border: 1px solid black;'
else:
return 'border-collapse: collapse; border: 1px solid black;'
df = df.style.applymap(format_color_groups)
#now df is styler so no need to add style
df = df.set_table_attributes('style="border-collapse: collapse; border: 1px solid black;"')
'Styler' object has no attribute 'hide_index' Error
BUG: AttributeError: type object 'object' has no attribute 'dtype' with numpy 1.20.x and pandas versions 1.0.4 and earlier
Styler object has no attribute 'map'
How to PERMANTELY format a number with commas to separate thousands
I am currently working on a project (using pandas & matplotlib) and one of the columns has large numbers that I would like to separate with a comma (i.e. separate the thousands place for each entry). This isn't required, but purely for aesthetics for myself as it makes it easier for me to view the data. Also, is this even okay to do or is considered bad craftsmanship to bother with it?
The code I'm currently using is:
df.head().style.format({'Income': '{:,.0f}'})
This works, but only changes the format for that single output. How do I make this change permanent for all future outputs?