2

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!

0

1 Answer 1

0

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.