-6

compare two different csv/excel files column "Name" and if both have same data than ignore that data and display the rest of output in new file.

File 1:

KeyField,Name,City,Zip,Location
123,Fred,Chicago,60558,A2
234,Mary,Orlando,12376,4L6
345,George,Boston, 40567,22
456,Peter,Topeka,00341,234
567,Doc,Birmingham,7654,H86
678,Isabel,Guadalajara,87654,M111

File 2:

KeyField,Name,City,Zip,Location
567,Doc,Birmingham,76543,H86
234,Michele,Orlando,12376,4L6
678,Isabel,Guadalajara,87654,U869
567,Doc,Birmingham,7654,H86
123,tony,Chicago,60558,A2
456,Peter,Topeka,00341,659

Output:File 3:

KeyField,Name,City,Zip,Location
123,Fred,Chicago,60558,A2
234,Mary,Orlando,12376,4L6
345,George,Boston, 40567,22
Srce Cde
  • 1,764
  • 10
  • 15
evil007
  • 3
  • 4

1 Answers1

0

You can use Python's library pandas:

1.) Read the CSV file in Pandas dataframe:

In [383]: df1 = pd.read_csv('f1.csv')

In [384]: df1
Out[384]: 
   KeyField    Name         City    Zip Location
0       123    Fred      Chicago  60558       A2
1       234    Mary      Orlando  12376      4L6
2       345  George       Boston  40567       22
3       456   Peter       Topeka    341      234
4       567     Doc   Birmingham   7654      H86
5       678  Isabel  Guadalajara  87654     M111

In [385]: df2 = pd.read_csv('f2.csv')

In [386]: df2
Out[386]: 
   KeyField     Name         City    Zip Location
0       567      Doc   Birmingham  76543      H86
1       234  Michele      Orlando  12376      4L6
2       678   Isabel  Guadalajara  87654     U869
3       567      Doc   Birmingham   7654      H86
4       123     tony      Chicago  60558       A2
5       456    Peter       Topeka    341      659

2.) Do a LEFT JOIN between df1 and df2. The records which don't find a match will have NULL ids.

In [392]: merged = pd.merge(df1,df2, on='Name', how='left')
In [396]: merged[merged.iloc[:,-1].isnull()][['KeyField_x','Name','City_x','Zip_x','Location_x']]
Out[396]: 
   KeyField_x    Name   City_x  Zip_x Location_x
0         123    Fred  Chicago  60558         A2
1         234    Mary  Orlando  12376        4L6
2         345  George   Boston  40567         22

So, the above are the records you wanted.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
  • Thank you! Mayank – evil007 Nov 26 '18 at 11:54
  • In output: File3 created and need to dispay raw (4,123,'tony','Chicago','60558','A2') as well because its not doublicate. I am sorry, as I have not mentioned this in main question – evil007 Nov 26 '18 at 13:10