如何验证两个不同的 .csv 文件列 ID 是否与 python 匹配?
How to verify that two different .csv files column ids match with python?
我有两个不同的 .csv
文件,但它们有相同的 id
列。
file_1.csv:
id, column1, column2
4543DFGD_werwe_23, string
4546476FGH34_wee_24, string
....
45sd234_w32rwe_2342342, string
另一个:
file_1.csv:
id, column3, column4
4543DFGD_werwe_23, bla bla bla
4546476FGH34_wee_24, bla bla bla
....
45sd234_w32rwe_2342342, bla bla bla
如何验证这两列与 csv 模块或 pandas 匹配(具有相同的 ID)或相同?
加载后您可以在 id 列上调用 equals
:
df['id'].equals(df1['id'])
这将 return True
of False
如果它们完全相同,长度和相同顺序的相同值
In [3]:
df = pd.DataFrame({'id':np.arange(10)})
df1 = pd.DataFrame({'id':np.arange(10)})
df.id.equals(df1.id)
Out[3]:
True
In [7]:
df = pd.DataFrame({'id':np.arange(10)})
df1 = pd.DataFrame({'id':[0,1,1,3,4,5,6,7,8,9]})
df.id.equals(df1.id)
Out[7]:
False
In [8]:
df.id == df1.id
Out[8]:
0 True
1 True
2 False
3 True
4 True
5 True
6 True
7 True
8 True
9 True
Name: id, dtype: bool
要加载 csvs:
df = pd.read_csv('file_1.csv')
df1 = pd.read_csv('file_2.csv') # I'm assuming your real other csv is not the same name as file_1.csv
然后就可以进行和上面一样的比较了:
df.id.equals(df1.id)
如果您只想比较 id 列,您可以指定只加载该列:
df = pd.read_csv('file_1.csv', usecols=['id'])
df1 = pd.read_csv('file_2.csv', usecols=['id'])
通过 csv 模块:
- 打开两个文件。
- Reader 文件通过
csv reader()
方法。
- 创建字典,因为行中的第一项是键,值是行。
- 使用
set
intersection
方法从字典中获取相同的键。
- 打印结果。
代码:
import csv
file1 = '/home/vivek/Desktop/Whosebug/fil1.csv'
file2 = '/home/vivek/Desktop/Whosebug/fil2.csv'
with open(file1) as fp1:
root = csv.reader(fp1)
rows1 = {}
for i in root:
rows1[i[0]]=i
if "id" in rows1:
del rows1["id"]
with open(file2) as fp1:
root = csv.reader(fp1)
rows2 = {}
for i in root:
rows2[i[0]]=i
if "id" in rows2:
del rows2["id"]
result = set(rows1.keys()).intersection(set(rows2.keys()))
print "Same Id :", list(result)
输出:
vivek@vivek:~/Desktop/Whosebug$ python 27.py
Same Id : ['4546476FGH34_wee_24', '4543DFGD_werwe_23', '45sd234_w32rwe_2342342']
我有两个不同的 .csv
文件,但它们有相同的 id
列。
file_1.csv:
id, column1, column2
4543DFGD_werwe_23, string
4546476FGH34_wee_24, string
....
45sd234_w32rwe_2342342, string
另一个:
file_1.csv:
id, column3, column4
4543DFGD_werwe_23, bla bla bla
4546476FGH34_wee_24, bla bla bla
....
45sd234_w32rwe_2342342, bla bla bla
如何验证这两列与 csv 模块或 pandas 匹配(具有相同的 ID)或相同?
加载后您可以在 id 列上调用 equals
:
df['id'].equals(df1['id'])
这将 return True
of False
如果它们完全相同,长度和相同顺序的相同值
In [3]:
df = pd.DataFrame({'id':np.arange(10)})
df1 = pd.DataFrame({'id':np.arange(10)})
df.id.equals(df1.id)
Out[3]:
True
In [7]:
df = pd.DataFrame({'id':np.arange(10)})
df1 = pd.DataFrame({'id':[0,1,1,3,4,5,6,7,8,9]})
df.id.equals(df1.id)
Out[7]:
False
In [8]:
df.id == df1.id
Out[8]:
0 True
1 True
2 False
3 True
4 True
5 True
6 True
7 True
8 True
9 True
Name: id, dtype: bool
要加载 csvs:
df = pd.read_csv('file_1.csv')
df1 = pd.read_csv('file_2.csv') # I'm assuming your real other csv is not the same name as file_1.csv
然后就可以进行和上面一样的比较了:
df.id.equals(df1.id)
如果您只想比较 id 列,您可以指定只加载该列:
df = pd.read_csv('file_1.csv', usecols=['id'])
df1 = pd.read_csv('file_2.csv', usecols=['id'])
通过 csv 模块:
- 打开两个文件。
- Reader 文件通过
csv reader()
方法。 - 创建字典,因为行中的第一项是键,值是行。
- 使用
set
intersection
方法从字典中获取相同的键。 - 打印结果。
代码:
import csv
file1 = '/home/vivek/Desktop/Whosebug/fil1.csv'
file2 = '/home/vivek/Desktop/Whosebug/fil2.csv'
with open(file1) as fp1:
root = csv.reader(fp1)
rows1 = {}
for i in root:
rows1[i[0]]=i
if "id" in rows1:
del rows1["id"]
with open(file2) as fp1:
root = csv.reader(fp1)
rows2 = {}
for i in root:
rows2[i[0]]=i
if "id" in rows2:
del rows2["id"]
result = set(rows1.keys()).intersection(set(rows2.keys()))
print "Same Id :", list(result)
输出:
vivek@vivek:~/Desktop/Whosebug$ python 27.py
Same Id : ['4546476FGH34_wee_24', '4543DFGD_werwe_23', '45sd234_w32rwe_2342342']