下面是我的代码:-
import pandas as pd
def highlight_diff_by_column_and_write(dataframe, column, file):
df = dataframe.copy()
for value in df[column].unique():
matches = df[df[column] == value]
for index, row in matches.iterrows():
for index2, row2 in matches.iterrows():
if index != index2:
for col in df.columns:
if row[col] != row2[col]:
df.at[index, col] = 'background-color: #FF0000'
df.at[index2, col] = 'background-color: #FF0000'
df.style.apply(lambda x: ['background: #FF0000' if x.name in matches.index else '' for i in x], axis=1).to_excel(file, engine='openpyxl')
file =('OMS-Diff-Reports_20221223.xlsx')
df = pd.read_excel(file,'TBPCRules')
highlight_diff_by_column_and_write(df, 'UNIQUE_ID', 'result.xlsx')
下面是我得到的结果:-x1c 0d1x
我想用背景色显示差异
1条答案
按热度按时间qxsslcnc1#
要使用
to_excel
方法突出显示dataframe
中的单元格的at
方法未进入Excel。必须使用创建Excel的库,如openpyxl