9

Whenever I try to perform any operation after styling in my code, I see this error:

AttributeError: 'Styler' object has no attribute 'drop'

In this instance I was trying to drop a column after applying a style, in other cases I tried concatenating 2 dataframes, though it throws a similar error. I'm very new to Pandas/Python programming.

For now I have tried to drop before I apply style, that works. But my requirement is to do this AFTER. Likewise I am trying to concatenate AFTER styling which it doesn't allow. I've reduced it to a very simple dataframe

Code:

df = pd.DataFrame([["A", 1],["B", 2]], columns=["Letter", "Number"])
def highlight(s):
    return ['background-color: red']
df = df.style.apply(highlight)
df = df.drop('Number', axis=1)  
with pd.ExcelWriter('testcolor.xlsx') as writer: 
    df.to_excel(writer,sheet_name = 'test')

Error:

AttributeError: 'Styler' object has no attribute 'drop'

I expect the column Number to be removed.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Rajesh D
  • 121
  • 1
  • 1
  • 5

1 Answers1

15

When you use style, df becomes a Styler object and it's not anymore a Dataframe object. You are trying to use Dataframe methods on a Styler object, and that will not work. The styler object contains the dataframe inside df.data, so you should do:

df = df.style.apply(highlight)
df.data = df.data.drop('Number', axis=1)     
micric
  • 621
  • 4
  • 15
  • Thanks so much , worked here. I'll now apply this to the actual DF I was working on . Much appreciated – Rajesh D Jun 18 '19 at 11:48
  • @RajeshD please consider [accepting](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) the answer if it works. :) – anky Jun 18 '19 at 11:57
  • I have a leading question regarding similar issue around concatenation. I created another dataframe df2 = pd.DataFrame([["A", 1],["B", 2]], columns=["Letter2", "Number2"]). I would like to concatenate this to the original dataframe. How can I achieve that ? – Rajesh D Jun 18 '19 at 12:53
  • what you mean with 'original dataframe'? The one before styling and dropping? – micric Jun 18 '19 at 13:13
  • Apologies , I mean the one that was modified after styling , so something like this df = pd.DataFrame([["A", 1],["B", 2]], columns=["Letter", "Number"]) df2 = pd.DataFrame([["A", 1],["B", 2]], columns=["Letter2", "Number2"]) def highlight(s): return ['background-color: red']*2 df = df.style.apply(highlight) df.data = df.data.drop('Letter', axis=1) combined = pd.concat([df.data, df2]) with pd.ExcelWriter('testcolor.xlsx') as writer: combined.to_excel(writer,sheet_name = 'test') I would expect Number to be red after I have dropped letter – Rajesh D Jun 18 '19 at 13:20
  • It's not very readable like that, it's better if you create a new question, if it's a new question – micric Jun 18 '19 at 13:37
  • Hi, although I have opened up a new question , based on the original answer, once you drop the column "Number", I observe the styling isn't retained. Is there a way to keep the style when I output to excel ? Thanks – Rajesh D Jun 18 '19 at 15:40
  • the style is retained. I think you are writing df.data instead of df. To be clear, df is the Style object, so the one with the style. df.data is just the data – micric Jun 18 '19 at 16:04
  • I used df too, still it doesn't retain. Wondering if there's something happening while writing to Excel ? – Rajesh D Jun 18 '19 at 16:21
  • As it was suggested in the other question, it's much better to perform all the operations first, then style the final dataframe – micric Jun 18 '19 at 16:28
  • Even in that discussion, the moment I try writing it to Excel, it loses the styling, so am thinking if its something wrong in that step ? – Rajesh D Jun 18 '19 at 16:39
  • This removes the formatting. I'm trying to highlight outlier's and I want the original scale of the gradient to be retained (i.e. set the colors, then filter out rows) – thistleknot May 17 '21 at 01:37