Home > database >  Pandas Setting all values in column in color
Pandas Setting all values in column in color

Time:12-11

For a new column in pandas, I want to set every value in red when I have the value 1. comp_df.loc[comp_df["comp_ad"] == 1].

I already tried it, but I have an error

comp_df.style.applymap(lambda x: "background-color: red" if comp_df['comp_ad'] == 1)

Thanks in advance!

CodePudding user response:

Use if-else statement with subset parameter for test column comp_ad:

(comp_df.style
        .applymap(lambda x: "background-color: red" if x == 1 else "", subset=['comp_ad'])
        .to_excel('styled.xlsx', engine='openpyxl'))
  • Related