繁体   English   中英

比较两个不同长度的CSV文件找到匹配值

[英]Compare two CSV files of different lengths to find matching values

我想比较两组数据并找到匹配项。 我有一个文件 (file1.csv) 1400 行,如下所示:

ID
123
456
789
145
165
175
185
195

我有另一个文件(file2.csv),大约 46,000 行,如下所示:

accountNumber, user, accountNumber2, address 
456,example@email.com,0,1 Lane
001,example1@email.com,175,2 Lane
002,example2@email.com,789,3 Lane
195,example3@email.com,0,4 Lane
123,example4@email.com,0,5 Lane
689,example5@email.com,0,6 Lane
003,example6@email.com,0,7 Lane
004,example7@email.com,0,8 Lane

我想使用 file1 ID 来匹配 file2 中的电子邮件。 我想要的 output 是一个新文件(file3),例如:

ID,Email
123, example4@email.com
456, example@email.com
789, example2@email.com
145, Not found
165, Not found
175, example1@email.com
185, Not found
195, example3@email.com

这是我尝试过的:

import pandas as pd

file1_df = pd.read_csv('file1.csv')
file2_df = pd.read_csv('file2.csv')

def search():
    for account_id in file1_df['account_id']:
        print("ID: ",account_id)
        id_loc = file1_df[file1_df['account_id'] == account_id].index.values
        print("id_loc",id_loc)

        try:
            accountNumber = file2_df[file2_df['accountNumber'] == account_id]['user'].values[0]
            print(accountNumber)
            accountNumber_loc = file2_df.loc[file2_df['accountNumber'] == account_id].index.values
            print(account_id, "Found at: ", accountNumber_loc)
            file1_df.loc[id_loc, "Located"] = accountNumber
        except Exception:
            pass
            try:
                accountNumber2 = file2_df[file2_df['accountNumber2'] == account_id]['user'].values[0]
                print(accountNumber2)
                accountNumber2_loc = file2_df.loc[file2_df['accountNumber2'] == account_id].index.values
                print(account_id, "Found at: ", accountNumber2_loc)
                file1_df.loc[id_loc, "Located"] = accountNumber2
            except Exception:
                print("Not Found")
                file1_df.loc[id_loc, "Located"] = "Not found"

search()
file1_df.to_csv('file3.csv')

我不断收到错误:

IndexError: index 0 is out of bounds for axis 0 with size 0

似乎它几乎适用于小文件,但一旦我尝试使用真实版本,我就会不断收到 IndexError。 有没有更好的方法来找到这些匹配项?

正如@Manakin 已经提到的,这只是您需要在 file1_df 作为左侧参考的简单连接操作。 添加了 rest 代码以获取所需格式的数据。

import pandas as pd

file1_df = pd.read_csv('file1.csv')
file2_df = pd.read_csv('file2.csv')

file3_df = pd.merge(file1_df, file2_df, left_on=['ID'], right_on=['accountNumber'], how='left')['id', 'accountNumber'].rename(columns = {'accountNumber': 'Email'})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM