简体   繁体   English

将两个csv文件的连接放在python的公共列上

[英]take a join of two csv files over a common column in python

I have two csv files which have following fields: 我有两个具有以下字段的csv文件:

FILE 1 : 文件1:

objectID,objectName,objecttype

FILE 2 : 文件2:

objectID,objectprice,objecttotalprice

the data in these two files is separated by , . 这两个文件中的数据用,分隔。 What I want is to take a join of these two files over objectID . 我想要的是通过objectID这两个文件。 The output should have joined data and the data of file 1 which did not matched with file 2. I tried this code but it is not giving correct output: 输出应该已经加入了数据,并且文件1的数据与文件2不匹配。我尝试了这段代码,但是没有给出正确的输出:

import pandas as pd

a = pd.read_csv("file1.csv", names = ["objectID", "objectName", "objecttype"],header = 0).astype(basestring)

    b = pd.read_csv("file1.csv").astype(basestring)

    merged= a.merge(b, on='objectID',how='outer')

    merged.to_csv("output.csv", index=False)

When I run this then in output I get data of file1 (with empty value for fields of file2 ) followed by data of file2 (with empty value for fields of file1 ). 当我运行这个再输出我得到的数据file1 (与字段为空值的file2 )其次是数据file2 (与字段为空值file1 )。

What am I doing wrong here and how can I do the join correctly 我在这里做错什么,如何正确进行联接

NOTE: In file1 the filed names are a bit different and hence I am renaming them when I am reading file1.csv above 注意:file1 ,文件名有些不同,因此当我在上面读取file1.csv时,我将其重命名

I think you are looking for a left join, try 我认为您正在寻找左联接,请尝试

merged= a.merge(b, on='objectID', how='left')

It works like SQL (see the documentation ) 它的工作方式类似于SQL(请参阅文档

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

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