【问题标题】:Looking up values of one csv file in another csv file and print the row在另一个 csv 文件中查找一个 csv 文件的值并打印该行
【发布时间】:2016-06-16 03:12:30
【问题描述】:

我有两个 csv 文件,一个是带有标题的单列,另一个是带有标题的多列。我想在一个列文件中查找值并在另一个文件中搜索具有相同标题的列。一旦找到匹配项,我希望打印整行。我知道这是一个 vlookup 函数,但是具有多列的 csv 文件非常大,当我尝试使用公式来完成此操作时,excel 总是崩溃。所以我一直在尝试使用python作为解决方案。

我正在接受这个专栏:

age
23
43
18

搜索此表:

Name,   age,number,AA,BB,CC,DD,EE
John,   23,   1,   34,35,36,37,38
Mary,   32,   2,   33,34,35,36,37
Jacob , 43,   3,   32,33,34,35,36
Matthew,22,   4,   31,32,33,34,35
Jean,   18,   5,   30,31,32,33,34

试图打印这个:

Name,   age,number,AA,BB,CC,DD,EE
John,   23,   1,   34,35,36,37,38
Jacob , 43,   3,   32,33,34,35,36
Jean,   18,   5,   30,31,32,33,34

我一直在尝试使用此代码,但我搞混了,它只是将第一行打印为一列:

with open('/home/s/Untitled 1.csv') as f:
r=pandas.read_csv(f)
with open('/home/s/Test1.csv','r') as w:
    x=pd.read_csv(w)
    col=w['age']
    for line in w:
        for col in w:
            for row in r:
                if row in col:
                    print(line)

我基本上希望脚本使用查询列中的第一个条目来搜索数据表中具有相同标题的列并打印该行,循环查找下面行中的其余条目。

任何建议将不胜感激!

【问题讨论】:

    标签: python csv pandas


    【解决方案1】:

    你的代码有很多问题,说明你很困惑。

    with open('/home/s/Untitled 1.csv') as f:
        r = pandas.read_csv(f)
    with open('/home/s/Test1.csv','r') as w:
        x = pandas.read_csv(w)
        # w is not indexable
        col = w['age']
        for line in w:
            # w is not a table.
            for col in w:
                for row in r:
                    if row in col:
                        print(line)
    

    我认为如果我为您分解问题会有所帮助:

    1. 读取第一个文件并存储年龄
    2. 读取第二个文件并存储在pandas数据帧中
    3. 循环遍历 pandas 数据框以查找年龄匹配项
    4. 打印匹配项

    读取第一个文件并存储年龄

    这可以通过使用列表而不是数据框来完成。稍后您将能够看到原因。

    ages = []
    with open("incsv1.csv", "r") as f:
        r = pandas.read_csv(f)
        ages = list(r["age"])
    

    读取第二个文件并将其存储在 pandas 数据框中

    你已经做到了:

    with open("incsv2.csv", "r") as f:
        x = pandas.read_csv(f)
    

    循环遍历 pandas 数据框以查找年龄匹配项并打印匹配项

    既然您知道您只会查看 ages 列,只需索引该列并遍历它:

    for i, age in enumerate(x["ages"]):
        # You can't do this without a numpy int64
        if age in ages:
            print x.loc[i]
    

    整个程序会输出:

    Name      John
    age         23
    number       1
    AA          34
    BB          35
    CC          36
    DD          37
    EE          38
    Name: 0, dtype: object
    Name      Jacob
    age           43
    number         3
    AA            32
    BB            33
    CC            34
    DD            35
    EE            36
    Name: 2, dtype: object
    Name      Jean
    age         18
    number       5
    AA          30
    BB          31
    CC          32
    DD          33
    EE          34
    Name: 4, dtype: object
    

    更好的方法

    现在,我知道您希望它以直线打印出来,所以我将向您展示我认为会更好的方法:

    import pandas
    
    ages = []
    with open("incsv1.csv", "r") as f:
        r = pandas.read_csv(f)
        ages = list(r["age"])
    
    with open("incsv2.csv", "r") as f:
        # Skip the first line
        f.readline()
        for line in f:
            if int(line.split(",")[1]) in ages:
                print line,
    

    如您所见,在这个问题中您并不需要pandas。事实上,我可以删除它:

    ages = []
    with open("incsv1.csv", "r") as f:
        # Skip the first line
        f.readline()
        for line in f:
            ages.append(int(line.strip("\n")))
    
    with open("incsv2.csv", "r") as f:
        # Skip the first line
        f.readline()
        for line in f:
            if int(line.split(",")[1]) in ages:
                print line,
    

    【讨论】:

    • 哇!那是一个快速的反应!你的回答很透彻,我真的很困惑。所以基本上在更好的方法中,您将年龄存储在数据框中(删除标题),然后读取 incsv2,然后声明如果第二列中的条目在“年龄”中以打印该行?
    • 是的,它就是这样做的。我忘了再提一件事:如果 csv 文件中有空格,pandas 将读取带有空格的标题。
    猜你喜欢
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2015-11-07
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多