I want to colour both the 'Grand Total' row and the 'Total' column in a DataFrame object df
, for which I tried the following:
import pandas as pd
# Function to set background highlight colour.
def bg_colour (val):
colour = '#ffff00'
return 'background-color: %s' % colour
df = pd.DataFrame({'Category': ['A','B','C','D','Grand Total'], 'Total': [1,2,3,4,10]})
t1 = df.style.applymap(bg_colour, subset = ['Total'])
dfT = df.T
dfT = dfT.style.applymap(bg_colour, subset = [4])
t1T = t1.T
However, when the compiler reaches the last line of the code, the following error arises:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-48-c0c380b9c518> in <module>
12 dfT = dfT.style.applymap(bg_colour, subset = [4])
13 display(dfT)
---> 14 t1T = t1.T
AttributeError: 'Styler' object has no attribute 'T'
From what I understand, style.applymap()
implicitly converts DataFrame into a Styler-type object, which cannot be further manipulated as a DataFrame.
Question 1: How can both rows and columns of a DataFrame object be coloured?
Question 2: How can a Styler-type object be converted into a DataFrame-type one?