Reputation: 317
I have two csv files.
one.csv:
1, 12.1455675, -13.1287564, 23, 9, 4.5, 4
2, 12.5934593, -13.0856385, 14, 5, 9.7, 6
3, 12.0496204, -13.8938582, 14, 6, 3.4, 9
4, 12.1456084, -12.1939589, 45, 2, 3.4, 8
two.csv:
9, 12.0496, -13.8939, .3, 55
3, 12.1456, -13.1288, 3.4, 9
What I want to do is match the two csv files based on columns one and two. I want another csv file that has the matched columns 1 and 2, but also includes the corresponding 3rd column values from two.csv and 6th column values from one.csv. Like this:
12.0496, -13.8939, 55, 3.4
12.1456, -12.1288, 9, 4.5
I am unsure how to go about this especially when some of the values in two.csv are rounded.
Any help is greatly appreciated!
Upvotes: 2
Views: 5093
Reputation: 414395
You could use pandas
' io to read/write csv files and its database-style joining/merging capabilities to merge the files:
import pandas as pd
normalize = lambda x: "%.4f" % float(x) # round
df = pd.read_csv("one.csv", index_col=(0,1), usecols=(1, 2, 5),
header=None, converters=dict.fromkeys([1,2], normalize))
df2 = pd.read_csv("two.csv", index_col=(0,1), usecols=(1, 2, 4),
header=None, converters=dict.fromkeys([1,2], normalize))
result = df.join(df2, how='inner')
result.to_csv("output.csv", header=None) # write as csv
12.0496,-13.8939,3.4,55
12.1456,-13.1288,4.5,9
Upvotes: 3
Reputation: 52000
This is a quite common question on SO.
As of myself, same answer: for a medium-term solution, import in a DB, then perform a query using a JOIN ...
Try a search: https://stackoverflow.com/search?q=combining+csv+python
Upvotes: 1