gpt4 book ai didi

python - 如何从 pandas diff 获取单元格位置?

转载 作者:太空宇宙 更新时间:2023-11-04 02:50:53 26 4
gpt4 key购买 nike

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)

我看过类似 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)

最佳答案

从两个 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))]

测试代码:

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))

结果:

    B  C
R2 2 3
R3 4 5

B C
R2 2 1
R3 4 5

['C2']

关于python - 如何从 pandas diff 获取单元格位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43921417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com