2016-06-10 58 views
1

这里我想比较两个excel文件。 Server_report有42列,Email_report有19列(其中5列与server_report完全不匹配)。每列中有14列匹配,但标题不同。当我打开这两个文件时,我会对三列进行排序,以便将数据排列为“交货”,“选择数量”,“批次”(按server_report排序)和“交货”,“采购数量”,“批量选择”(根据email_report排序)。使用Pandas DataFrames比较两个不同头文件但使用相同行数据的Excel文件

我需要的是将排序后的email_report与server_report进行比较(每个文件具有相同的行数并且可以在'Delivery'列进行索引)。如果server_report上存在“缺失”信息,则需要使用从email_report获取的信息来填充该信息。

之后,需要生成两个新文件。

  1. 一个新的server_report,其中包含所有原始42列,并且具有来自email_report的更改。

  2. 一个包含比较过程中所做更改的新文件。

我的问题在这里是这篇文章的标题。如何比较具有不同列/标题的两个文件(不是所有可以映射到另一个的文件)

+0

这两个报告是否有相同的行数?两个报告中通用的每行是否有唯一的ID? – andrew

+0

这应该是每个工作表具有相同数量的行的情况。每个工作表共享第一个A列的完全相同的标题名称(Index?)和编号(数据)。 –

回答

0

在此解决方案中,我假设两个报告具有相同数量的行和索引。

import copy 
import pandas as pd 
import numpy as np 

# Example reports 
email_report = pd.DataFrame({'a':np.arange(6), 'b':np.arange(6), 'c':['A', 'B', 

email_report 

>>> 
'C', 'D', 'E', 'F'], 'extra_col':np.zeros(6)}) 
    a b c extra_col 
0 0 0 A  0.0 
1 1 1 B  0.0 
2 2 2 C  0.0 
3 3 3 D  0.0 
4 4 4 E  0.0 
5 5 5 F  0.0 

server_report = pd.DataFrame({'d':np.append(np.random.random_integers(0,5,5),5), 
    'e':np.append(np.random.random_integers(0, 5, 5),5), 
    'f':['A', 'B', 'C', 'D', 'E', 'F'], 
    'g':['a', 'b', 'c', 'd', 'e', 'f']}) 

server_report 
>>> 
    d e f g 
0 0 2 A a 
1 1 0 B b 
2 3 3 C c 
3 1 3 D d 
4 5 4 E e 
5 5 5 F f 

# Identify the columns of interest in both reports and map between them 
col_map = {'d':'a', 'e':'b', 'f':'c'} 
server_report_cols = ['d', 'e', 'f'] 
email_report_cols = [col_map[c] for c in server_report_cols] 

现在我们运行差异..

# Run the diff report 
def report_diff(x): 
    return x[0] if x[0] == x[1] else '{} ---> {}'.format(*x) 

def make_diff(df1, df2, df1_cols, df2_cols): 
    """ 
    I am assuming that df1_cols and df2_cols are both sorted in the same order. 
    """ 
    temp = pd.concat([df1[df1_cols], df2[df2_cols]], axis=1) 
    diff_rows = [] 
    for row in temp.iterrows(): 
     diff_rows.append([report_diff((row[1][x[0]], row[1][x[1]])) for x in zip(df1_cols, df2_cols)]) 
    diff = copy.deepcopy(df2) 
    diff[df2_cols] = pd.DataFrame(diff_rows, columns=df2_cols) 
    return diff 

diff = make_diff(email_report, server_report, email_report_cols, server_report_cols) 
print diff 
>>> 
      d   e f g 
0 0 ---> 2 0 ---> 5 A a 
1 1 ---> 0 1 ---> 4 B b 
2 2 ---> 1 2 ---> 0 C c 
3 3 ---> 0 3 ---> 2 D d 
4 4 ---> 5   4 E e 
5   5   5 F f 

并创建两个输出文件。

# Get a boolean series indicating which rows will be changed 
change_check = ~(email_report[email_report_cols] == server_report[server_report_cols]. 
    rename(columns=col_map)).all(axis=1) 

# Output_1: Corrected "server report" 
server_report[server_report_cols] = email_report[email_report_cols] # Overwrite the server report 
server_report.to_excel('my-diff-sap.xlsx',index=False) 

# Output_2: Record of corrections using the headers from the server report 
diff[change_check].to_excel('changed_rows.xlsx', index=False) 

print server_report 
>>> 
    d e f g 
0 0 0 A a 
1 1 1 B b 
2 2 2 C c 
3 3 3 D d 
4 4 4 E e 
5 5 5 F f 

print diff[change_check] 
>>> 
      d   e f g 
0 0 ---> 2 0 ---> 1 A a 
1 1 ---> 0 1 ---> 5 B b 
2 2 ---> 0   2 C c 
3 3 ---> 5 3 ---> 4 D d 
4 4 ---> 1   4 E e 
相关问题