简体   繁体   English

在两个数据框差异之间应用 Pandas 样式

[英]Applying Pandas style between two dataframes differences

I've seen a lot of questions about finding the differences between two pandas dataframes, however here i'm trying to apply a Pandas.Style difference between two dataframes.我已经看到很多关于寻找两个Pandas数据帧之间差异的问题,但是在这里我试图应用两个数据帧之间的Pandas.Style差异。 Given these two example dataframes i'm hoping to have a formatter applied to right[1, "B"] and right["D"] because they're different that lefts values or new in general:鉴于这两个示例数据帧,我希望将格式化程序应用于 right[1, "B"] 和 right["D"] ,因为它们与 left 值或 new 一般不同:

left = pd.DataFrame([[1,1,1], [2,2,2]], columns=list("ABC"))
right = pd.DataFrame([[1,1,10], [2,5,10]], columns=list("ABD"))

Here's my idea for the formatting method guided by the pandas documentation这是我对 Pandas 文档指导的格式化方法的想法

def formatter(s, new):
    if s.name not in new.columns:
        # column doesn't exist strike through entire thing
        return "color: red; text-decoration: line-through;"

    elif not s.equals(new[s.name]):
        # apply per value a comparision of the elements
        # for val in s: 
            # if val != right[val.index??]:
                return "color: red; text-decoration: line-through;"

    return "color: black;"

left.style.apply(formatter, args=(right))

My thinking is that afterwards I should have something that is html like such:我的想法是之后我应该有一些像这样的html:

 <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>A</th> <th>B</th> <th>C</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <th>1</th> <td>2</td> <td>2</td> <td>2</td> </tr> </tbody> </table>

 <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>A</th> <th>B</th> <th>C</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>1</td> <td>1</td> <td style="color: red; text-decoration: line-through;">10</td> </tr> <tr> <th>1</th> <td>2</td> <td style="color: red; text-decoration: line-through;">5</td> <td style="color: red; text-decoration: line-through;">10</td> </tr> </tbody> </table>

It is a bit unclear exactly where you are a stuck, but the code is not far off.有点不清楚你到底卡在哪里,但代码不远了。

This might be what you are after:这可能是您所追求的:

left = pd.DataFrame([[1,1,1], [2,2,2]], columns=list("ABC"))
right = pd.DataFrame([[1,1,10], [2,5,10]], columns=list("ABD"))
def formatter(s, new):
    if s.name not in new.columns:
        # column doesn't exist strike through entire thing
        return ["color: red; text-decoration: line-through;"]*len(s)

    elif not s.equals(new[s.name]):
        return ["color: red; text-decoration: line-through;" if v else "" for v in s == new[s.name]]

    return ["color: black;"]*len(s)

left.style.apply(formatter, args=[right])

The formatter method now returns data in the same shape as the input (as per the docs). formatter 方法现在以与输入相同的形状返回数据(根据文档)。

The right dataframe is passed as a list and not a tuple.正确的数据帧作为列表而不是元组传递。

Also changed the per value comparison to return color if they differ, otherwise keep default style.还更改了每个值的比较以在它们不同时返回颜色,否则保留默认样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM