2016-11-10 146 views
1
file 1 
column: id name city work 
row: 123 Mark Chicago baker 

file 2 
column: id name work age 
row: 123 Mark baker 27 

我想匹配两个文件添加在输出列“年龄”。如何匹配2个文件csv?

file output 

column: id name city  work  age 
row: 123 Mark Chicago baker  27 

你能帮助我吗?

回答

1

您可以使用pandaslibrary像这样

import pandas 

def main(): 
file1 = pandas.read_csv("file1.csv") 
file2 = pandas.read_csv("file2.csv") 
file2 = file2.dropna(axis=1) 
output = file1.merge(file2, on='id') 
output.to_csv("output.csv", index=False) 

if __name__ == "__main__": main() 

希望这将帮助你(Y)。

+0

是的,这是非常有用的,但程序只打印第一行。你知道我怎么打印所有的数据库? – francy672