数据框中的样式(着色)

2024-05-17 07:15:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在基于唯一id比较两个数据帧,并在excel中显示如下结果-

enter image description here

我使用highlight_diff函数(请参见代码中的内容)来突出显示差异,但它表示 ValueError(“非唯一索引不支持样式”)

import pandas as pd
import sys
import csv
import numpy as np

file1 = 'C:/Users/anagpurk/Desktop/backup/testoutput/test_act.csv'
file2 = 'C:/Users/anagpurk/Desktop/backup/testoutput/test_exp.csv'

df = pd.read_csv(file1,header=0,sep=r'\u0001', engine='python') 

df2 = pd.read_csv(file2,header=0,sep=r'\u0001', engine='python')

df_all = pd.concat([df.set_index('loop_vendor_id'), df2.set_index('loop_vendor_id')], 
                   axis='columns', keys=['First', 'Second'])

df_final = df_all.swaplevel(axis='columns')[df.columns[1:]]


def highlight_diff(data, color='yellow'):
    attr = 'background-color: {}'.format(color)
    other = data.xs('First', axis='columns', level=-1)
    return pd.DataFrame(np.where(data.ne(other, level=0), attr, ''),
                        index=data.index, columns=data.columns)

df_final.style.apply(highlight_diff, axis=None)

writer = pd.ExcelWriter('C:/Users/anagpurk/Desktop/backup/testoutput/output.xlsx', 
engine='xlsxwriter')
df_final.to_excel(writer, sheet_name = 'Sheet1',index=True)
writer.save()
writer.close()

Tags: columnscsvimportiddfdataindexdiff