Pandas create a new column after dataframe gets df.style

Viewed 185

I'm trying to add a new column after dataframe gets df.style. However, I got an error message:

'Styler' object does not support item assignment

Below is my code:

import pandas as pd
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))

subsets = pd.IndexSlice[:, 'A']
df2 = df.style.applymap(lambda x: 'background-color: yellow', subset = subsets)
df2['sum'] = None

Below is what I want to achieve:

Link

How can I convert df2 to a real dataframe that I can edit? Thank you!

1 Answers

You cannot processing styler object by add data processing like add new column, you need add it before:

df2['sum'] = None
subsets = pd.IndexSlice[:, 'A']
df2 = df.style.applymap(lambda x: 'background-color: yellow', subset = subsets)
Related