简体   繁体   中英

How to write dataframe to excel with conditional formatting in Python?

How to export pandas dataframe to excel with conditional formatting?

Sample Data

from random import randint

x = [randint(0, 1) for p in range(0, 10)]

sample_dict = {"Col1": [randint(0, 1) for p in range(0, 10)],
               "Col2": [randint(0, 1) for p in range(0, 10)],
               "Col3": [randint(0, 1) for p in range(0, 10)],
               "Col4": [randint(0, 1) for p in range(0, 10)],
               "Col5": [randint(0, 1) for p in range(0, 10)],
               "Col6": [randint(0, 1) for p in range(0, 10)]}

sample = pd.DataFrame(sample_dict)

  Col1  Col2    Col3    Col4    Col5    Col6
0   1   1   0   1   0   0
1   0   1   1   0   1   1
2   1   0   1   0   0   1
3   1   1   0   1   0   0
4   1   0   0   1   1   1
5   0   0   1   1   0   0
6   1   1   0   0   0   0
7   0   0   1   0   1   1
8   0   1   1   1   0   0
9   0   1   1   0   0   1

Required Conditional Formatting in pandas styler

sample.style.apply(lambda x: ["background: orange" if v != x.iloc[0] else "" for v in x], axis = 1)

在此处输入图像描述

Apart from the Pandas styler you could use Excel's conditional formatting to get a similar, but dynamic, effect. For example:

import pandas as pd
from random import randint

x = [randint(0, 1) for p in range(0, 10)]

sample_dict = {"Col1": [randint(0, 1) for p in range(0, 10)],
               "Col2": [randint(0, 1) for p in range(0, 10)],
               "Col3": [randint(0, 1) for p in range(0, 10)],
               "Col4": [randint(0, 1) for p in range(0, 10)],
               "Col5": [randint(0, 1) for p in range(0, 10)],
               "Col6": [randint(0, 1) for p in range(0, 10)]}

sample = pd.DataFrame(sample_dict)


# 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.
sample.to_excel(writer, sheet_name='Sheet1')

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

# Add a format.
format1 = workbook.add_format({'bg_color': 'orange'})

# Get the dimensions of the dataframe.
(max_row, max_col) = sample.shape

# Apply a conditional format to the required cell range.
worksheet.conditional_format(1, 1, max_row, max_col,
                             {'type':     'formula',
                              'criteria': '=$B2<>B2',
                              'format':   format1})

# Close the Pandas Excel writer and output the Excel file.
writer.save()

Output :

在此处输入图像描述

I changed the background: orange to background-color: orange , if you use background-color: none or simple "" in your else statement doesn't effect the output. See:

from random import randint
import pandas as pd

x = [randint(0, 1) for p in range(0, 10)]

sample_dict = {"Col1": [randint(0, 1) for p in range(0, 10)],
               "Col2": [randint(0, 1) for p in range(0, 10)],
               "Col3": [randint(0, 1) for p in range(0, 10)],
               "Col4": [randint(0, 1) for p in range(0, 10)],
               "Col5": [randint(0, 1) for p in range(0, 10)],
               "Col6": [randint(0, 1) for p in range(0, 10)]}

sample = pd.DataFrame(sample_dict)

sample = sample.style.apply(lambda x: ["background-color: orange" if v != x.iloc[0] else "background_color: none" for v in x], axis=1)
sample.to_excel('sample.xlsx', engine='openpyxl')

This will give you:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM