Skip to main content
 首页 » 编程设计

python之在另一个 csv 文件中查找一个 csv 文件的值并打印该行

2024年10月17日6leader

我有两个 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) 

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

如有任何建议,我们将不胜感激!

请您参考如下方法:

你的代码有很多问题,说明你很糊涂。

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,