#Values to States states_map = { 1:'AL',30:'MT',2:'AK',31:'NE',4:'AZ',32:'NV',5:'AR',33:'NH', 6:'CA',34:'NJ',8:'CO',35:'NM',9:'CT',36:'NY',10:'DE',37:'NC', 11:'DC',38:'ND',12:'FL',39:'OH',13:'GA',40:'OK',15:'HI',41:'OR', 16:'ID',42:'PA',17:'IL',44:'RI',18:'IN',45:'SC',19:'IA',46:'SD', 20:'KS',47:'TN',21:'KY',48:'TX',22:'LA',49:'UT',23:'ME',50:'VT', 24:'MD',51:'VA',25:'MA',53:'WA',26:'MI',54:'WV',27:'MN',55:'WI', 28:'MS',56:'WY',29:'MO' } #This value and higher will be considered RICH RICH_FLAG = 15 # $100,000 or more #This value and higher will be considered EDUCATED EDUCATED_FLAG = 43 # Bachelor's Degree or higher #Values to Races #for simplicity I will group basic races WHITE = 1 BLACK = 2 NATIVE = 3 #American Indian, Alaskan Native ASIAN = 4 ISLANDER = 5 #Hawaiian/Pacific Islander MULTI_RACIAL = 6 #Multi-Racial >= 6 #Create the lists we need to store the data we will retrieve #from the Census data dictionary income = [] education = [] ethnicity = [] state = [] #to hold counts per state state_count = {} #to store values # RICH AND EDUCATED [W, B, AI, A, H, M # RICH AND NON-EDUCATED # NON-RICH AND EDUCATED # NON-RICH AND NON-EDUCATED # total] state_count_template = [0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0] with open('jun15pub.dat') as data_file: for line in data_file.readlines(): inc = int(line[38:40]) edu = int(line[136:138]) eth = int(line[138:140]) st = states_map[int(line[92:94])] income.append(inc) education.append(edu) ethnicity.append(eth) state.append(st) if st not in state_count: state_count[st] = state_count_template[:] #total count in states of all categories state_count[st][24] += 1 #RICH AND EDUCATED if inc >= RICH_FLAG and edu >= EDUCATED_FLAG: if eth == WHITE: #White Only state_count[st][0] += 1 elif eth == BLACK: #Black Only state_count[st][1] += 1 elif eth == NATIVE: #American Indian, Alaskan Native Only state_count[st][2] += 1 elif eth == ASIAN: #Asian Only state_count[st][3] += 1 elif eth == ISLANDER: #Hawaiian/Pacific Islander Only state_count[st][4] += 1 else: # Multi-Racial state_count[st][5] += 1 #RICH AND NON-EDUCATED elif inc >= RICH_FLAG and edu < EDUCATED_FLAG: if eth == WHITE: state_count[st][6] += 1 elif eth == BLACK: state_count[st][7] += 1 elif eth == NATIVE: state_count[st][8] += 1 elif eth == ASIAN: state_count[st][9] += 1 elif eth == ISLANDER: state_count[st][10] += 1 else: state_count[st][11] += 1 #NON-RICH AND EDUCATED elif inc < RICH_FLAG and edu >= EDUCATED_FLAG: if eth == WHITE: state_count[st][12] += 1 elif eth == BLACK: state_count[st][13] += 1 elif eth == NATIVE: state_count[st][14] += 1 elif eth == ASIAN: state_count[st][15] += 1 elif eth == ISLANDER: state_count[st][16] += 1 else: state_count[st][17] += 1 #NON-RICH AND NON-EDUCATED else: if eth == WHITE: state_count[st][18] += 1 elif eth == BLACK: state_count[st][19] += 1 elif eth == NATIVE: state_count[st][20] += 1 elif eth == ASIAN: state_count[st][21] += 1 elif eth == ISLANDER: state_count[st][22] += 1 else: state_count[st][23] += 1 #Use these to store each group count to be displayed #with its corresponding state #RICH AND EDUCATED displayAW = "" displayAB = "" displayAI = "" displayAA = "" displayAH = "" displayAM = "" for state in state_count: #store the percentage of population group of each state groupAW = state_count[state][0]/float(state_count[state][24]) groupAB = state_count[state][1]/float(state_count[state][24]) groupAI = state_count[state][2]/float(state_count[state][24]) groupAA = state_count[state][3]/float(state_count[state][24]) groupAH = state_count[state][4]/float(state_count[state][24]) groupAM = state_count[state][5]/float(state_count[state][24]) #store these states with group value #these will be used as a tooltip on our map displayAW += "['US-%s', %f]," % (state,groupAW) displayAB += "['US-%s', %f]," % (state,groupAB) displayAI += "['US-%s', %f]," % (state,groupAI) displayAA += "['US-%s', %f]," % (state,groupAA) displayAH += "['US-%s', %f]," % (state,groupAH) displayAM += "['US-%s', %f]," % (state,groupAM) print 'RICH AND EDUCATED WHITE: ' print displayAW print 'RICH AND EDUCATED BLACK: ' print displayAB print 'RICH AND EDUCATED NATIVE: ' print displayAI print 'RICH AND EDUCATED ASIAN: ' print displayAA print 'RICH AND EDUCATED ISLANDER: ' print displayAH print 'RICH AND EDUCATED MULTI: ' print displayAM #Use these to store each group count to be displayed #with its corresponding state #RICH AND NON-EDUCATED displayBW = "" displayBB = "" displayBI = "" displayBA = "" displayBH = "" displayBM = "" for state in state_count: #store the percentage of population group of each state groupBW = state_count[state][6]/float(state_count[state][24]) groupBB = state_count[state][7]/float(state_count[state][24]) groupBI = state_count[state][8]/float(state_count[state][24]) groupBA = state_count[state][9]/float(state_count[state][24]) groupBH = state_count[state][10]/float(state_count[state][24]) groupBM = state_count[state][11]/float(state_count[state][24]) #store these states with group value #these will be used as a tooltip on our map displayBW += "['US-%s', %f]," % (state,groupBW) displayBB += "['US-%s', %f]," % (state,groupBB) displayBI += "['US-%s', %f]," % (state,groupBI) displayBA += "['US-%s', %f]," % (state,groupBA) displayBH += "['US-%s', %f]," % (state,groupBH) displayBM += "['US-%s', %f]," % (state,groupBM) print 'RICH AND NON-EDUCATED WHITE: ' print displayBW print 'RICH AND NON-EDUCATED BLACK: ' print displayBB print 'RICH AND NON-EDUCATED NATIVE: ' print displayBI print 'RICH AND NON-EDUCATED ASIAN: ' print displayBA print 'RICH AND NON-EDUCATED ISLANDER: ' print displayBH print 'RICH AND NON-EDUCATED MULTI: ' print displayBM #Use these to store each group count to be displayed #with its corresponding state #NON-RICH AND EDUCATED displayCW = "" displayCB = "" displayCI = "" displayCA = "" displayCH = "" displayCM = "" for state in state_count: #store the percentage of population group of each state groupCW = state_count[state][12]/float(state_count[state][24]) groupCB = state_count[state][13]/float(state_count[state][24]) groupCI = state_count[state][14]/float(state_count[state][24]) groupCA = state_count[state][15]/float(state_count[state][24]) groupCH = state_count[state][16]/float(state_count[state][24]) groupCM = state_count[state][17]/float(state_count[state][24]) #store these states with group value #these will be used as a tooltip on our map displayCW += "['US-%s', %f]," % (state,groupCW) displayCB += "['US-%s', %f]," % (state,groupCB) displayCI += "['US-%s', %f]," % (state,groupCI) displayCA += "['US-%s', %f]," % (state,groupCA) displayCH += "['US-%s', %f]," % (state,groupCH) displayCM += "['US-%s', %f]," % (state,groupCM) print 'NON-RICH AND EDUCATED WHITE: ' print displayCW print 'NON-RICH AND EDUCATED BLACK: ' print displayCB print 'NON-RICH AND EDUCATED NATIVE: ' print displayCI print 'NON-RICH AND EDUCATED ASIAN: ' print displayCA print 'NON-RICH AND EDUCATED ISLANDER: ' print displayCH print 'NON-RICH AND EDUCATED MULTI: ' print displayCM #Use these to store each group count to be displayed #with its corresponding state #NON-RICH AND NON-EDUCATED displayDW = "" displayDB = "" displayDI = "" displayDA = "" displayDH = "" displayDM = "" for state in state_count: #store the percentage of population group of each state groupDW = state_count[state][18]/float(state_count[state][24]) groupDB = state_count[state][19]/float(state_count[state][24]) groupDI = state_count[state][20]/float(state_count[state][24]) groupDA = state_count[state][21]/float(state_count[state][24]) groupDH = state_count[state][22]/float(state_count[state][24]) groupDM = state_count[state][23]/float(state_count[state][24]) #store these states with group value #these will be used as a tooltip on our map displayDW += "['US-%s', %f]," % (state,groupDW) displayDB += "['US-%s', %f]," % (state,groupDB) displayDI += "['US-%s', %f]," % (state,groupDI) displayDA += "['US-%s', %f]," % (state,groupDA) displayDH += "['US-%s', %f]," % (state,groupDH) displayDM += "['US-%s', %f]," % (state,groupDM) print 'NON-RICH AND NON-EDUCATED WHITE: ' print displayDW print 'NON-RICH AND NON-EDUCATED BLACK: ' print displayDB print 'NON-RICH AND NON-EDUCATED NATIVE: ' print displayDI print 'NON-RICH AND NON-EDUCATED ASIAN: ' print displayDA print 'NON-RICH AND NON-EDUCATED ISLANDER: ' print displayDH print 'NON-RICH AND NON-EDUCATED MULTI: ' print displayDM #create google map with the data we set def write_html(grp,rep): html = "" with open('map.html') as f: for line in f.readlines(): if 'REPLACE_ME' in line: html += rep else: html += line with open("index-%s.html" % (grp),"w") as f: f.write(html) write_html('aw',displayAW) write_html('ab',displayAB) write_html('ai',displayAI) write_html('aa',displayAA) write_html('ah',displayAH) write_html('am',displayAM) from IPython.display import IFrame IFrame('index-aw.html', width=700, height=400) from IPython.display import IFrame IFrame('index-ab.html', width=700, height=400) from IPython.display import IFrame IFrame('index-ai.html', width=700, height=400) from IPython.display import IFrame IFrame('index-aa.html', width=700, height=400) from IPython.display import IFrame IFrame('index-ah.html', width=700, height=400) from IPython.display import IFrame IFrame('index-am.html', width=700, height=400) #create google map with the data we set def write_html(grp,rep): html = "" with open('map.html') as f: for line in f.readlines(): if 'REPLACE_ME' in line: html += rep else: html += line with open("index-%s.html" % (grp),"w") as f: f.write(html) write_html('bw',displayBW) write_html('bb',displayBB) write_html('bi',displayBI) write_html('ba',displayBA) write_html('bh',displayBH) write_html('bm',displayBM) from IPython.display import IFrame IFrame('index-bw.html', width=700, height=400) from IPython.display import IFrame IFrame('index-bb.html', width=700, height=400) from IPython.display import IFrame IFrame('index-bi.html', width=700, height=400) from IPython.display import IFrame IFrame('index-ba.html', width=700, height=400) from IPython.display import IFrame IFrame('index-bh.html', width=700, height=400) from IPython.display import IFrame IFrame('index-bm.html', width=700, height=400) #create google map with the data we set def write_html(grp,rep): html = "" with open('map.html') as f: for line in f.readlines(): if 'REPLACE_ME' in line: html += rep else: html += line with open("index-%s.html" % (grp),"w") as f: f.write(html) write_html('cw',displayCW) write_html('cb',displayCB) write_html('ci',displayCI) write_html('ca',displayCA) write_html('ch',displayCH) write_html('cm',displayCM) from IPython.display import IFrame IFrame('index-cw.html', width=700, height=400) from IPython.display import IFrame IFrame('index-cb.html', width=700, height=400) from IPython.display import IFrame IFrame('index-ci.html', width=700, height=400) from IPython.display import IFrame IFrame('index-ca.html', width=700, height=400) from IPython.display import IFrame IFrame('index-ch.html', width=700, height=400) from IPython.display import IFrame IFrame('index-cm.html', width=700, height=400) #create google map with the data we set def write_html(grp,rep): html = "" with open('map.html') as f: for line in f.readlines(): if 'REPLACE_ME' in line: html += rep else: html += line with open("index-%s.html" % (grp),"w") as f: f.write(html) write_html('dw',displayDW) write_html('db',displayDB) write_html('di',displayDI) write_html('da',displayDA) write_html('dh',displayDH) write_html('dm',displayDM) from IPython.display import IFrame IFrame('index-dw.html', width=700, height=400) from IPython.display import IFrame IFrame('index-db.html', width=700, height=400) from IPython.display import IFrame IFrame('index-di.html', width=700, height=400) from IPython.display import IFrame IFrame('index-da.html', width=700, height=400) from IPython.display import IFrame IFrame('index-dh.html', width=700, height=400) from IPython.display import IFrame IFrame('index-dm.html', width=700, height=400)