I have two CSV files that look like this..
CSV 1
JavaScript
x
7
1
reference | name | house
2
----------------------------
3
2348A | john | 37
4
5648R | bill | 3
5
RT48 | kate | 88
6
76A | harry | 433
7
CSV2
JavaScript
1
5
1
reference
2
---------
3
2348A
4
76A
5
Using Python and CSVkit I am trying to create an output CSV of the rows in CSV1 by comparing it to CSV2. Does anybody have an example they can point me in the direction of?
Advertisement
Answer
I would recommended to use pandas
to achieve what you are looking for:
And here is how simple it would be using pandas, consider your two csv files are like this:
CSV1
JavaScript
1
6
1
reference,name,house
2
2348A,john,37
3
5648R,bill,3
4
RT48,kate,88
5
76A,harry ,433
6
CSV2
JavaScript
1
4
1
reference
2
2348A
3
76A
4
Code
JavaScript
1
6
1
import pandas as pd
2
df1 = pd.read_csv(r'd:tempdata1.csv')
3
df2 = pd.read_csv(r'd:tempdata2.csv')
4
df3 = pd.merge(df1,df2, on= 'reference', how='inner')
5
df3.to_csv('outpt.csv')
6
output.csv
JavaScript
1
4
1
,reference,name,house
2
0,2348A,john,37
3
1,76A,harry ,433
4