Using Nested Dictionaries in Python -- combining two datasets with a common 'key' and different other variables -


i have 2 csv datasets county level data. each dataset identifies county fips code. want create nested 'master' dictionary such can call identifying fips code , return corresonding 'inner' dictionary fips, contains information both datasets.

i understand general way set nested dictionaries, namely:

>>> d = {} >>> d['dict1'] = {} >>> d['dict1']['innerkey'] = 'value' >>> d {'dict1': {'innerkey': 'value'}} 

but don't know how generalize , populate data read in 2 separate csvs.

say define master dictionary as:

master = {} 

first, iterate on smaller data set foo can populate master dictionary using fips code key, , storing data under 'foo' key:

for row in foo_csv_reader:     fips_code = row[...] # row storing fips code.     inner_data = {}     inner_data['foo'] = ... # data foo csv.     master[fips_code] = inner_data 

now, iterate on larger data set bar can populate master dictionary using matching fips code encountered previous data set, , storing data under 'bar' key:

for row in bar_csv_reader:     fips_code = row[...] # row storing fips code.     if fips_code in master:         inner_data = master[fips_code]         inner_data['bar'] = ... # data bar csv. 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -