gpt4 book ai didi

python - 用 python pandas 比较两个 csv 文件

转载 作者:太空狗 更新时间:2023-10-30 00:48:03 27 4
gpt4 key购买 nike

我有两个 csv 文件,都包含两列。

第一个是产品id,第二个是序列号。

我需要查找第一个 csv 中的所有序列号,并在第二个 csv 上找到匹配项。结果报告将在单独的列中包含匹配的序列号以及每个 csv 中的相应产品 ID我真的修改了下面的代码,没有运气。

你会如何处理这个问题?

import pandas as pd
A=set(pd.read_csv("c1.csv", index_col=False, header=None)[0]) #reads the csv, takes only the first column and creates a set out of it.
B=set(pd.read_csv("c2.csv", index_col=False, header=None)[0]) #same here
print(A-B) #set A - set B gives back everything thats only in A.
print(B-A) # same here, other way around.

最佳答案

我想你需要merge :

A = pd.DataFrame({'product id':   [1455,5452,3775],
'serial number':[44,55,66]})

print (A)

B = pd.DataFrame({'product id': [7000,2000,1000],
'serial number':[44,55,77]})

print (B)

print (pd.merge(A, B, on='serial number'))
product id_x serial number product id_y
0 1455 44 7000
1 5452 55 2000

关于python - 用 python pandas 比较两个 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42418180/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com