Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pandas比较两个具有不同行值和坐标的excel电子表格_Python_Excel_Pandas - Fatal编程技术网

Python 使用pandas比较两个具有不同行值和坐标的excel电子表格

Python 使用pandas比较两个具有不同行值和坐标的excel电子表格,python,excel,pandas,Python,Excel,Pandas,我正在制作一个与熊猫比较的excel程序。我制作了一个简单的比较工具,效果很好,但它可以逐行比较,并显示列的其他部分中出现的更改。这是因为两张图纸中的行坐标不相等。为了澄清一点,以下是我的代码: import pandas as pd import numpy as np import openpyxl wb = openpyxl.load_workbook('CK_CBF_Draft_01.2018_original.xlsx') ws = wb['CBF'] list1 = [] fo

我正在制作一个与熊猫比较的excel程序。我制作了一个简单的比较工具,效果很好,但它可以逐行比较,并显示列的其他部分中出现的更改。这是因为两张图纸中的行坐标不相等。为了澄清一点,以下是我的代码:

import pandas as pd
import numpy as np
import openpyxl

wb = openpyxl.load_workbook('CK_CBF_Draft_01.2018_original.xlsx')
ws = wb['CBF']

list1 = []

for i in ws['H1':'H365']:
    for cell in i:
        list1.append(i)

# Define the diff function to show the changes in each field
def report_diff(x):
    return x[1] if x[1] in list1 else '{} ---> {}'.format(x[0],x[1])

# We want to be able to easily tell which rows have changes
def has_change(row):
    if "--->" in row.to_string():
        return "Y"
    else:
        return "N"

# Read in both excel files
df1 = pd.read_excel('Invoice1.xlsx', 'Sheet1', na_values=['NA'])
df2 = pd.read_excel('Invoice2.xlsx', 'Sheet1', na_values=['NA'])

# Make sure we order by account number so the comparisons work
df1.sort_values(by="Host Name")
df1=df1.reindex()
df2.sort_values(by="Host Name")
df2=df2.reindex()

# Create a panel of the two dataframes
diff_panel = pd.Panel(dict(df1=df1,df2=df2))

#Apply the diff function
diff_output = diff_panel.apply(report_diff, axis=0)

# Flag all the changes
diff_output['has_change'] = diff_output.apply(has_change, axis=1)

#Save the changes to excel but only include the columns we care about
diff_output[(diff_output.has_change == 'Y')].to_excel('my-diff-1.xlsx',index=False,columns=["Host Name","CPU#","Memory","Invoice Total","Quantity"])

print('Worked')
正如我所说的,问题在于这会返回一行一行的差异,而这些差异是不正确的,因为它们出现在列的不同部分。有人知道一种精确比较两个不同行的文件的方法吗

谢谢你的帮助,如果问题有点模糊,我很抱歉

import pandas as pd
import numpy as np

# Read both Excel files
file1 = pd.read_excel("file1.xlsx", na_values=['NA'])
file2 = pd.read_excel("file2.xlsx", na_values=['NA'])


df2 = file1
df1 = file2


res = df1[df1['samecolname'].isin(df2['samecolname'].unique())]
                   
res2 = df2[df2['samecolname'].isin(df1['samecolname'].unique())]               

res.to_excel('diff1-insecond-but-not-in-first.xlsx',index=False)
res2.to_excel('diff2-in-first-not-in-second.xlsx',index=False)