如何从pandas差异中获取细胞位置?

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]),所以我可以通过它:

 redFill = PatternFill(start_color='FFEE1111', end_color='FFEE1111', fill_type='solid') lsws['A1'].fill = redFill lsfh.save(sfcn) 

我已经看过这样的解决scheme,但我不能得到它的工作/不理解它。 例如,以下不起作用:

 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) 

要从两个pandas.DataFrame获取差异单元格作为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))] 

testing代码:

 import pandas as pd df1 = pd.read_excel('test.xlsx') print(df1) df2 = df.copy() df2.C['R2'] = 1 print(df2) print(diff_cell_indices(df1, df2)) 

结果:

  BC R2 2 3 R3 4 5 BC R2 2 1 R3 4 5 ['C2']