熊猫数据框中的样式(着色)

时间:2020-01-15 11:24:30

标签: python pandas dataframe

我正在基于唯一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()

0 个答案:

没有答案