如何使用 Python 中的条件格式将数据帧写入 excel?

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

如何使用条件格式将 pandas 数据帧导出到 excel?

示例数据

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

pandas 样式器

中要求的条件格式
sample.style.apply(lambda x: ["background: orange" if v != x.iloc[0] else "" for v in x], axis = 1)

我将 background: orange 更改为 background-color: orange,如果您在 else 语句中使用 background-color: none 或简单的 "" 不会影响输出。参见:

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')

这会给你:

除了 Pandas 样式器之外,您还可以使用 Excel 的条件格式来获得类似但动态的效果。例如:

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()

输出: