1

我正在尝试使用我的 highlight_diff 函数来比较两个数据帧并突出显示 HTML 中的差异。问题发生在输出到 html 时。我在另一篇文章中读到有人通过使用 Pandas 样式器成功地将格式设置为应用于书面 html。样式器正确地形成表格,但我也想添加表格边框。谁能指出我正确的方向?

def highlight_diff(data, color='yellow'):
        attr = 'background-color: {}'.format(color)
        other = data.xs('Old', axis='columns', level=0)
        return pd.DataFrame(np.where(data.ne(other, level=-1), attr, ''),
                            index=data.index, columns=data.columns)
html=dfs.style.apply(highlight_diff, axis=None).render()
#I have tried:
#dfs.style.set_table_styles([{'selector': 'th, td', 'props': [('border', '1px solid black'), 
                                # ('padding', '4px'), 
                                # ('text-align', 'center')]}])
with open(fname, 'w', encoding='utf-8') as myFile:
        myFile.write(html)
4

0 回答 0