TypeError: 'Styler' object is not subscriptable

Viewed 7664

I can access a subset of columns like this:

df[[5, 6]]

..but after the following line to push text to the left and make it more readable:

df = df.style.set_properties(**{'text-align': 'left'})

..the same command produces an error:

TypeError: 'Styler' object is not subscriptable

Is this expected behaviour, or do you need to use some trick now to get to the columns?

Am using Python 3.7.6 and Pandas 1.0.3

1 Answers

Styler does not return a dataframe, but a style object. From there, you should/could use apply functions to format your dataframe printout. For example:

 # apply style on the columns
 df.style.apply(lambda x: ["text-align:right"]*len(x))
Related