I have a set of 100 files. 50 files containing census information for each US state. The other fifty are geographic data that need to be merged with the correct file for each state.
For each state, the census file and its corresponding geo file are related by a common variable, LOGRECNO, that is the 10th column in the census file and the 7th column in the geo file.
The problem is that the geo file has more rows than the census file; my census data does not cover certain subsets of geographic locations and hence has fewer rows than the geo data file.
How can I merge the census data with the geographic date (keeping only the rows/geo locations where census data exists, don't care about the rest)?
I am a newbie to Python and I somewhat know how to do basic csv file i/o in python. Manipulating 2 csvs at the same time is proving confusing.
Example:
sample_state_census.csv
Varname 1 Varname 2 ... Varname 10 (LOGRECNO) ... Varname 16000
xxx xxx ... 1 ... xxx
xxx xxx ... 2 ... xxx
...
...
xxx xxx ... 514 ... xxx
xxx xxx ... 1312 ... xxx
...
...
xxx xxx ... 1500 ... xxx
sample_state_geo.csv
GeoVarname 1 GeoVarname 2 ... GeoVarname 7 (LOGRECNO) ... GeoVarname 65
yyy yyy ... 1 ... yyy
yyy yyy ... 2 ... yyy
...
...
yyy yyy ... 514 ... yyy
yyy yyy ... 515 ... yyy
...
...
yyy yyy ... 1500 ... yyy
Expected output (don't merge rows for values of LOGRECNO that don't exist in sample_state_census.csv)
Varname 1 Varname 2 ... Varname 10 (LOGRECNO) GeoVarname 1 GeoVarname 2 ... GeoVarname 65 Varname 11... Varname 16000
xxx xxx ... 1 yyy yyy ... yyy xxx ... xxx
xxx xxx ... 2 yyy yyy ... yyy xxx ... xxx
...
...
xxx xxx ... 514 yyy yyy ... yyy xxx ... xxx
xxx xxx ... 1312 yyy yyy ... yyy xxx ... xxx
...
...
xxx xxx ... 1500 yyy yyy ... yyy xxx ... xxx
For merging multiple files (even > 2) based on one or more common columns, one of the best and efficient approaches in python would be to use "brewery". You could even specify what fields need to be considered for merging and what fields need to be saved.
Create list of all fields and add filename to store information
about origin of data records
Go through source definitions and collect the fields
Read data from the shorter file into memory, into a dictionary keyed on the
LOGRECNO
row:then use this dictionary to match against the geo data, write out matches:
This all assumes the census file is not so big as to take up too much memory. If that's the case you'll have to use a database instead (read all data into a SQLite database, match in the same vein agains the geo data).