python - 在 Python 中比较黄金标准 csv 文件和提取值 csv 文件

标签 python regex csv

这是一项数据挖掘任务,我们将自动对提取的质量进行评分。 有一个黄金标准 csv,可能包含如下所示的字段

golden_standard.csv

| id | description             | amount  | date       |
|----|-------------------------|---------|------------|
| 1  | Some description.       | $150.54 | 12/12/2012 |
| 2  | Some other description. | $200    | 10/10/2015 |
| 3  | Other description.      | $25     | 11/11/2014 |
| 4  | My description          | $11.35  | 01/01/2015 |
| 5  | Your description.       | $20     | 03/03/2013 |

,然后有两个可能的提取结果文件:

提取1.csv

| id | description             | date       |
|----|-------------------------|------------|
| 1  | Some description.       | 12/12/2012 |
| 2  | Some other description. | 10/10/2015 |
| 3  | Other description.      | 11/11/2014 |
| 4  | 122333222233332221      | 11/11/2014 |
| 5  | Your description.       | 03/03/2013 |

提取2.csv

| id | description             | amount  | date       |
|----|-------------------------|---------|------------|
| 1  | Some description.       | $150.54 | 12/12/2012 |
| 2  | Some other description. | $200    | 10/10/2015 |
| -  | ----------------------- | -----   | ---------- |
| 5  | Your description.       | $20     | 03/03/2013 |

extract3.csv

| Garbage  | More Garbage       |
| Garbage  | More Garbage       | 

我希望我的程序报告提取 1 缺少一列,并且第 2 列中的值未正确匹配。

对于第二种情况,我缺少条目并且某些行都不匹配。

在最后一种情况下,生成的 csv 完全搞砸了,但我仍然希望程序能够检测到一些有意义的异常。

有人有一些快速而聪明的方法在Python中进行这种比较吗?

我有我可以在此处发布的常规、较长的逐行和逐列迭代方式,但我认为可能有一种更快、更优雅的 Python 方式来做到这一点。

非常感谢任何帮助。

最佳答案

免责声明:我的方法使用 pandas 库。

首先,数据设置。

gold_std.csv

id,description,amount,date
1,Some description.,$150.54,12/12/2012
2,Some other description.,$200,10/10/2015
3,Other description.,$25,11/11/2014
4,My description,$11.35,01/01/2015
5,Your description.,$20,03/03/2013

extract1.csv

id,description,date
1,Some description.,12/12/2012
2,Some other description.,10/10/2015
3,Other description.,11/11/2014
4,122333222233332221,11/11/2014
5,Your description.,03/03/2013

extract2.csv

id,description,amount,date
1,Some description.,$150.54,12/12/2012
2,Some other description.,$200,10/10/2015
3,Other description.,$25,11/11/2014
5,Your description.,$20,03/03/2013

第二,代码。

import pandas as pd

def compare_extract(extract_name, reference='gold_std.csv'):

    gold = pd.read_csv(reference)
    ext = pd.read_csv(extract_name)

    gc = set(gold.columns)
    header = ext.columns
    extc = set(header)

    if gc != extc:
        missing = ", ".join(list(gc - extc))
        print "Extract has the following missing columns: {}".format(missing)
    else:
        print "Extract has the same column as standard. Checking for abberant rows..."
        gold_list = gold.values.tolist()
        ext_list = ext.values.tolist()
        # Somewhat non-pandaic approach because possible no same IDs so we're relying
        # on set operations instead. A bit hackish, actually.
        diff = list(set(map(tuple, gold_list)) - set(map(tuple, ext_list)))
        df = pd.DataFrame(diff, columns=header)
        print "The following rows are not in the extract: "
        print df

第三,测试运行。

e1 = 'extract1.csv'
compare_extract(e1)
# Extract has the following missing columns: amount

e2 = 'extract2.csv'
compare_extract(e2)
# Extract has the same column as standard. Checking for abberant rows...
# The following rows are not in the extract: 
#    id     description  amount        date
# 0   4  My description  $11.35  01/01/2015

最后,最后的摘录有点随意。我认为对于这个问题,你最好编写一个非 pandas 算法。

关于python - 在 Python 中比较黄金标准 csv 文件和提取值 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31173917/

相关文章:

regex - 计算文本文件中的单个单词

Java正则表达式替换双引号内的双引号

python - 如何在堆叠小部件中重用另一个页面中的项目 - pyqt

python - 即使在 pretty_print=True 时,使用 lxml 编写也不会发出空格

python - Scapy 中的 HTTP GET 数据包嗅探器

javascript - 如何在小于 O(n) 的时间内找到数组中出现次数最多的数字?

python - 将值循环到列表中(更改值)

sql-server - 在 255 个字符处停止查询输出到 CSV 换行

mysql - 将csv文件加载到服务器上的mysql中

csv - 同时写入多个 csv 文件,在 Golang 中的分区列上拆分