如何从Pandasdiff获得细胞定位?

2024-05-29 03:17:46 发布

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

df1 = pd.read_excel(mxln)  # Loads master xlsx for comparison
df2 = pd.read_excel(sfcn)  # Loads student xlsx for comparison
difference = df2[df2 != df1]  # Scans for differences

无论哪里有区别,我都希望将这些单元格位置存储在一个列表中。它的格式必须是'A1'(而不是[1,1]),这样我就可以通过以下方式传递它:

^{pr2}$

我研究过类似this的解决方案,但我无法使其发挥作用/不理解它。例如,以下命令不起作用:

def highlight_cells():
    df1 = pd.read_excel(mxln)  # Loads master xlsx for comparison
    df2 = pd.read_excel(sfcn)  # Loads student xlsx for comparison
    difference = df2[df2 != df1]  # Scans for differences
    return ['background-color: yellow']

df2.style.apply(highlight_cells) 

Tags: masterforreadxlsxexcelstudentcomparisonpd
1条回答
网友
1楼 · 发布于 2024-05-29 03:17:46

要将两个^{}的差异单元格作为excel坐标,可以执行以下操作:

代码:

def diff_cell_indices(dataframe1, dataframe2):
    from openpyxl.utils import get_column_letter as column_letter

    x_ofs = dataframe1.columns.nlevels + 1
    y_ofs = dataframe1.index.nlevels + 1
    return [column_letter(x + x_ofs) + str(y + y_ofs) for
            y, x in zip(*np.where(dataframe1 != dataframe2))]

测试代码:

^{pr2}$

结果:

    B  C
R2  2  3
R3  4  5

    B  C
R2  2  1
R3  4  5

['C2']

相关问题 更多 >

    热门问题