1

I am trying to color values of one column in my df based on its values. I tried with only one value but I don't get any colored result. Here is my code

writer = pd.ExcelWriter('resultcolored.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet')
workbook = writer.book
worksheet = writer.sheets["Sheet"]



format = workbook.add_format({'bg_color':   '#C6EFCE',
                               'font_color': '#006100'})
worksheet.conditional_format( 'AJ1:AJ10',
    {   'type': 'text',
        'criteria': 'containing',
        'value': 'Direct',
        'format': format
    }
)
writer.save()

It saves the file, but doesn't colot any of the values. Can you please help me out?

1 Answer 1

1

It should work as expected. Here is a working example based in your code:

import pandas as pd


# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Data': ['Foo', 'Director', 'bar', 'Directory', 'Baz']})

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_conditional.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')

# Get the xlsxwriter workbook and worksheet objects.
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

format = workbook.add_format({'bg_color':   '#C6EFCE',
                              'font_color': '#006100'})

# Apply a conditional format to the cell range.
worksheet.conditional_format( 'B2:B6',
    {   'type': 'text',
        'criteria': 'containing',
        'value': 'Direct',
        'format': format
    }
)

writer.save()

Output:

enter image description here

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.