使用Pandas DataFrames比较具有不同标题但具有相同行数据的两个Excel文件

肖恩·施罗德(Sean Schroder)

在这里,我试图比较两个Excel文件。Server_report有42列,Email_report有19列(其中5列与server_report完全不匹配)。每个报表中有14列匹配,但标题不同。当我打开两个文件时,我会在三列上进行排序,以将数据按“交货”,“拣配数量”,“批次”(在server_report上排序)和“交货”,“拣配数量”,“批次拣配”(在email_report上排序)。

我需要的是排序后进行排序后将email_report与server_report进行比较(每个文件具有相同的行数,并且可以在“传递”列上建立索引)。如果server_report上有“丢失”的信息,则需要用从email_report获得的信息来填写。

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

  1. 一个新的server_report,其中所有原始42列均包含email_report中的更改。

  2. 一个新文件,其中包含在比较期间所做的任何更改。

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

安德鲁

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

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]

现在我们运行diff ..

# 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Python Pandas比较具有不同行数的两个Excel文件

使用两个具有不同行数的选择查询插入数据

使用Excel,如何比较两个计划(行具有相同的代码,但字符数不同)

比较Pandas中的两个Excel文件,并在两个列中返回具有相同值的行

使用Pandas从具有不同行长的文件导入数据

需要使用python合并两个具有相同列数但标题不同的csv文件

尝试使用Powershell计算Excel在两个不同的列中具有特定文本的行数

如何使用AssertJ递归比较具有不同字段名称但具有相同值的两个对象

使用具有许多列的awk比较两个文件,并获得数据不同的列

如何在数据框(Pandas)中查找具有相同行名的两个单元格的总和

使用 awk 加入 2 个具有不同行数的文件第 2 部分

使用cmake比较具有不同行尾的文件

使用awk合并具有相同列的两个文件的数据

使用两个循环来循环两个变量,以生成R中具有不同数据和工作表名称的Excel文件

当使用 Excel VBA 有两个具有相同 TagName 和 InnerText 的不同链接时,如何单击所需的链接

如何使用BASH比较两个文本文件是否具有相同的精确文本?

将两个具有相同形状和相同列名的Pandas数据框相乘

如何合并两个具有相同属性的 Pandas 数据框并覆盖相同的行?

比较两个具有不同行数的数据集的表达式

Swift类使用两个在不同目标中具有相同名称的Objective C文件

使用一列比较具有相同索引的两个数据框

pandas concat比较具有相同列名的2个数据框如何构建不同的列?

合并具有相同列和不同列的两个Pandas DataFrame

使用R比较两个不同行的数据帧

使用roxygen2在R包中具有相同的功能但具有两个不同的名称吗?

如何合并(使用 DataFrame)具有相同输入但顺序不同的两个数据集

是否可以使用 Perl 合并具有不同行数的数据框?

在具有相同属性的两个不同对象上使用equal()方法

结合两个具有相同索引的Pandas数据框