In [15]:
import urllib2
import xmltodict
from string import split
from secrets import wolfram_key

states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington State', 'West Virginia', 'Wisconsin', 'Wyoming', 'Washington DC']
def query(q, key=wolfram_key):
    return urllib2.urlopen('http://api.wolframalpha.com/v2/query?appid=%s&input=%s&format=plaintext' % (key,urllib2.quote(q))).read()

def state_query(group_name, states=states):
    results = {s: xmltodict.parse(query('number of %s in %s?' % (group_name, s))) for s in states}
    results = {s: [p for p in v['queryresult']['pod'] if p['@title'] == 'Result'][0]['subpod']['plaintext'] for s,v in results.items()}
    results = {s: float(split(v)[0])*(1000000 if 'million' in v else 1) for s,v in results.items()}
    return results
    
In [17]:
adults = state_query('adults')
In [19]:
for group in ['phds', 'college graduates']:
    group_data = state_query(group)
    print '-'*100
    print group
    print '\n'.join('#%d: %s: %2.2f%%' % (i+1,r[0],r[1]) for i,r in enumerate(sorted({s: (group_data[s]/adults[s])*100 for s in states}.items(), key = lambda x: x[1], reverse=True)))
----------------------------------------------------------------------------------------------------
phds
#1: Washington DC: 3.68%
#2: Maryland: 2.32%
#3: Massachusetts: 2.30%
#4: New Mexico: 1.75%
#5: Vermont: 1.66%
#6: Connecticut: 1.60%
#7: Delaware: 1.56%
#8: Virginia: 1.54%
#9: California: 1.42%
#10: New Jersey: 1.41%
#11: Rhode Island: 1.39%
#12: Colorado: 1.37%
#13: Oregon: 1.35%
#14: Washington State: 1.34%
#15: Pennsylvania: 1.31%
#16: New York: 1.30%
#17: Hawaii: 1.25%
#18: New Hampshire: 1.24%
#19: Arizona: 1.18%
#20: Utah: 1.17%
#21: Minnesota: 1.16%
#22: Montana: 1.14%
#23: North Carolina: 1.13%
#24: Illinois: 1.13%
#25: Nebraska: 1.12%
#26: Maine: 1.12%
#27: Florida: 1.10%
#28: Alaska: 1.10%
#29: Kansas: 1.08%
#30: Tennessee: 1.05%
#31: Missouri: 1.05%
#32: Georgia: 1.04%
#33: Idaho: 1.02%
#34: Wyoming: 1.02%
#35: Iowa: 1.02%
#36: Wisconsin: 0.99%
#37: Michigan: 0.98%
#38: South Dakota: 0.97%
#39: Ohio: 0.97%
#40: Texas: 0.94%
#41: South Carolina: 0.93%
#42: Alabama: 0.92%
#43: Indiana: 0.91%
#44: North Dakota: 0.87%
#45: Mississippi: 0.82%
#46: Oklahoma: 0.81%
#47: Louisiana: 0.81%
#48: Kentucky: 0.80%
#49: Nevada: 0.78%
#50: Arkansas: 0.77%
#51: West Virginia: 0.77%
----------------------------------------------------------------------------------------------------
college graduates
#1: Washington DC: 50.47%
#2: Massachusetts: 48.28%
#3: Connecticut: 45.71%
#4: New Hampshire: 44.77%
#5: Colorado: 44.07%
#6: Vermont: 43.98%
#7: New Jersey: 43.76%
#8: Maryland: 43.53%
#9: Minnesota: 42.94%
#10: Hawaii: 41.90%
#11: Washington State: 41.71%
#12: Virginia: 41.58%
#13: New York: 40.38%
#14: Rhode Island: 39.72%
#15: North Dakota: 39.42%
#16: Maine: 39.06%
#17: Illinois: 39.05%
#18: Oregon: 39.04%
#19: Florida: 38.78%
#20: Nebraska: 38.50%
#21: Montana: 38.27%
#22: Kansas: 38.26%
#23: California: 38.13%
#24: Delaware: 37.30%
#25: South Dakota: 37.15%
#26: Iowa: 36.75%
#27: Wisconsin: 36.73%
#28: Pennsylvania: 36.66%
#29: Utah: 36.56%
#30: Arizona: 36.26%
#31: North Carolina: 35.96%
#32: Michigan: 34.98%
#33: Wyoming: 34.34%
#34: New Mexico: 34.09%
#35: Georgia: 33.89%
#36: South Carolina: 33.78%
#37: Idaho: 33.74%
#38: Ohio: 33.65%
#39: Missouri: 33.54%
#40: Alaska: 33.04%
#41: Texas: 31.96%
#42: Indiana: 31.04%
#43: Oklahoma: 30.72%
#44: Tennessee: 30.29%
#45: Alabama: 30.18%
#46: Nevada: 30.18%
#47: Kentucky: 28.44%
#48: Mississippi: 27.99%
#49: Arkansas: 26.74%
#50: Louisiana: 26.32%
#51: West Virginia: 25.49%

In [26]:
 
----------------------------------------------------------------------------------------------------
college graduates
#1: Georgia: 48.46%
#2: Washington: 34.74%
#3: Massachusetts: 30.56%
#4: New Hampshire: 28.79%
#5: Connecticut: 28.43%
#6: Vermont: 28.40%
#7: Maryland: 27.32%
#8: Colorado: 27.30%
#9: New Jersey: 27.21%
#10: Minnesota: 26.53%
#11: New York: 26.30%
#12: Virginia: 26.05%
#13: Hawaii: 25.79%
#14: Rhode Island: 25.56%
#15: Maine: 24.80%
#16: Illinois: 24.34%
#17: Oregon: 24.11%
#18: North Dakota: 23.99%
#19: California: 23.52%
#20: Montana: 23.42%
#21: Florida: 23.11%
#22: Nebraska: 23.08%
#23: Kansas: 23.00%
#24: Wisconsin: 22.76%
#25: Delaware: 22.74%
#26: Pennsylvania: 22.63%
#27: Iowa: 22.15%
#28: Michigan: 22.01%
#29: South Dakota: 21.91%
#30: North Carolina: 21.84%
#31: Arizona: 21.26%
#32: Wyoming: 21.04%
#33: Ohio: 20.88%
#34: Alaska: 20.84%
#35: Utah: 20.61%
#36: South Carolina: 20.55%
#37: Missouri: 20.55%
#38: New Mexico: 20.34%
#39: Idaho: 19.68%
#40: Indiana: 19.03%
#41: Texas: 18.89%
#42: Tennessee: 18.60%
#43: Nevada: 18.54%
#44: Alabama: 18.50%
#45: Oklahoma: 18.40%
#46: Kentucky: 17.61%
#47: Mississippi: 17.05%
#48: Louisiana: 16.04%
#49: Arkansas: 16.02%
#50: West Virginia: 15.94%
----------------------------------------------------------------------------------------------------
latinos
#1: New Mexico: 43.90%
#2: California: 35.71%
#3: Texas: 34.74%
#4: Arizona: 28.00%
#5: New York: 27.90%
#6: Nevada: 24.75%
#7: Florida: 20.96%
#8: Colorado: 19.22%
#9: Georgia: 18.60%
#10: New Jersey: 16.64%
#11: Illinois: 15.07%
#12: Connecticut: 12.49%
#13: Rhode Island: 11.84%
#14: Utah: 11.60%
#15: Oregon: 10.85%
#16: Idaho: 10.18%
#17: Kansas: 9.62%
#18: Massachusetts: 8.88%
#19: Washington: 8.53%
#20: Hawaii: 8.40%
#21: Nebraska: 8.25%
#22: Wyoming: 8.06%
#23: Oklahoma: 7.97%
#24: North Carolina: 7.49%
#25: Maryland: 7.38%
#26: Delaware: 7.38%
#27: Virginia: 7.10%
#28: Arkansas: 5.73%
#29: Indiana: 5.55%
#30: Wisconsin: 5.44%
#31: Alaska: 5.31%
#32: Pennsylvania: 5.15%
#33: Iowa: 4.47%
#34: South Carolina: 4.46%
#35: Minnesota: 4.37%
#36: Michigan: 4.29%
#37: Tennessee: 4.08%
#38: Louisiana: 3.78%
#39: Alabama: 3.38%
#40: Missouri: 3.31%
#41: Ohio: 2.89%
#42: Montana: 2.73%
#43: Kentucky: 2.69%
#44: New Hampshire: 2.68%
#45: South Dakota: 2.48%
#46: Mississippi: 2.34%
#47: North Dakota: 1.88%
#48: Vermont: 1.46%
#49: Maine: 1.28%
#50: West Virginia: 1.13%
----------------------------------------------------------------------------------------------------
adults
#1: Georgia: 143.01%
#2: Washington: 68.84%
#3: New York: 65.13%
#4: Vermont: 64.58%
#5: Rhode Island: 64.34%
#6: New Hampshire: 64.31%
#7: Maine: 63.48%
#8: Massachusetts: 63.28%
#9: Alaska: 63.08%
#10: Michigan: 62.93%
#11: Maryland: 62.75%
#12: Virginia: 62.64%
#13: West Virginia: 62.53%
#14: Illinois: 62.35%
#15: Connecticut: 62.19%
#16: New Jersey: 62.17%
#17: Ohio: 62.05%
#18: Wisconsin: 61.96%
#19: Colorado: 61.95%
#20: Kentucky: 61.94%
#21: Minnesota: 61.78%
#22: Oregon: 61.75%
#23: Pennsylvania: 61.73%
#24: California: 61.69%
#25: Hawaii: 61.56%
#26: Nevada: 61.44%
#27: Tennessee: 61.41%
#28: Alabama: 61.32%
#29: Indiana: 61.30%
#30: Wyoming: 61.26%
#31: Missouri: 61.25%
#32: Montana: 61.19%
#33: Delaware: 60.97%
#34: Louisiana: 60.94%
#35: Mississippi: 60.93%
#36: South Carolina: 60.85%
#37: North Dakota: 60.84%
#38: North Carolina: 60.74%
#39: Iowa: 60.29%
#40: Kansas: 60.12%
#41: Nebraska: 59.96%
#42: Arkansas: 59.94%
#43: Oklahoma: 59.89%
#44: New Mexico: 59.65%
#45: Florida: 59.60%
#46: Texas: 59.10%
#47: South Dakota: 58.98%
#48: Arizona: 58.61%
#49: Idaho: 58.33%
#50: Utah: 56.37%
----------------------------------------------------------------------------------------------------
whites
#1: Georgia: 137.16%
#2: Maine: 95.56%
#3: Vermont: 95.29%
#4: New Hampshire: 94.23%
#5: West Virginia: 93.53%
#6: Iowa: 90.56%
#7: Idaho: 88.77%
#8: Wyoming: 87.71%
#9: Montana: 87.61%
#10: North Dakota: 87.35%
#11: Kentucky: 86.82%
#12: Nebraska: 86.16%
#13: Wisconsin: 86.05%
#14: Minnesota: 84.94%
#15: Utah: 84.35%
#16: South Dakota: 84.01%
#17: Indiana: 83.80%
#18: Kansas: 83.32%
#19: Ohio: 83.18%
#20: Oregon: 83.16%
#21: Rhode Island: 82.46%
#22: Missouri: 82.15%
#23: Pennsylvania: 82.10%
#24: Massachusetts: 80.28%
#25: Michigan: 79.94%
#26: Colorado: 79.79%
#27: Connecticut: 78.08%
#28: Arkansas: 76.72%
#29: Tennessee: 76.70%
#30: Arizona: 75.34%
#31: Florida: 74.08%
#32: Oklahoma: 71.73%
#33: Nevada: 71.17%
#34: Illinois: 70.99%
#35: Delaware: 69.06%
#36: New Mexico: 68.88%
#37: New Jersey: 68.86%
#38: Alabama: 68.58%
#39: Texas: 68.17%
#40: Virginia: 67.68%
#41: North Carolina: 66.84%
#42: South Carolina: 64.84%
#43: Alaska: 64.22%
#44: Louisiana: 61.62%
#45: California: 59.41%
#46: Mississippi: 59.15%
#47: Maryland: 58.27%
#48: New York: 43.66%
#49: Washington: 36.96%
#50: Hawaii: 24.28%
----------------------------------------------------------------------------------------------------
phds
#1: Washington: 2.53%
#2: Georgia: 1.48%
#3: Maryland: 1.46%
#4: Massachusetts: 1.46%
#5: Vermont: 1.08%
#6: New Mexico: 1.05%
#7: Connecticut: 0.99%
#8: Virginia: 0.97%
#9: Delaware: 0.95%
#10: Rhode Island: 0.90%
#11: California: 0.88%
#12: New Jersey: 0.88%
#13: New York: 0.85%
#14: Colorado: 0.85%
#15: Oregon: 0.83%
#16: Pennsylvania: 0.81%
#17: New Hampshire: 0.80%
#18: Hawaii: 0.77%
#19: Minnesota: 0.72%
#20: Maine: 0.71%
#21: Illinois: 0.71%
#22: Montana: 0.70%
#23: Alaska: 0.69%
#24: Arizona: 0.69%
#25: North Carolina: 0.69%
#26: Nebraska: 0.67%
#27: Utah: 0.66%
#28: Florida: 0.66%
#29: Kansas: 0.65%
#30: Tennessee: 0.64%
#31: Missouri: 0.64%
#32: Wyoming: 0.62%
#33: Michigan: 0.62%
#34: Wisconsin: 0.62%
#35: Iowa: 0.61%
#36: Ohio: 0.60%
#37: Idaho: 0.59%
#38: South Dakota: 0.57%
#39: South Carolina: 0.57%
#40: Alabama: 0.56%
#41: Indiana: 0.56%
#42: Texas: 0.56%
#43: North Dakota: 0.53%
#44: Mississippi: 0.50%
#45: Kentucky: 0.50%
#46: Louisiana: 0.50%
#47: Oklahoma: 0.49%
#48: West Virginia: 0.48%
#49: Nevada: 0.48%
#50: Arkansas: 0.46%
----------------------------------------------------------------------------------------------------
african americans
#1: Georgia: 68.08%
#2: Washington: 51.36%
#3: Mississippi: 36.52%
#4: Louisiana: 30.73%
#5: Maryland: 28.57%
#6: South Carolina: 27.12%
#7: Alabama: 25.65%
#8: New York: 24.87%
#9: North Carolina: 20.52%
#10: Delaware: 20.42%
#11: Virginia: 18.92%
#12: Tennessee: 16.20%
#13: Florida: 15.22%
#14: Arkansas: 15.15%
#15: Illinois: 14.45%
#16: Michigan: 14.20%
#17: New Jersey: 13.33%
#18: Ohio: 12.05%
#19: Missouri: 11.30%
#20: Texas: 11.16%
#21: Pennsylvania: 10.57%
#22: Connecticut: 9.57%
#23: Indiana: 8.78%
#24: Nevada: 7.62%
#25: Kentucky: 7.57%
#26: Oklahoma: 7.05%
#27: Massachusetts: 6.41%
#28: Wisconsin: 6.10%
#29: California: 5.96%
#30: Rhode Island: 5.91%
#31: Kansas: 5.63%
#32: Minnesota: 4.81%
#33: Nebraska: 4.23%
#34: Colorado: 3.72%
#35: Arizona: 3.71%
#36: West Virginia: 3.18%
#37: Alaska: 3.13%
#38: Iowa: 2.70%
#39: New Mexico: 1.92%
#40: Oregon: 1.72%
#41: Hawaii: 1.54%
#42: South Dakota: 1.08%
#43: New Hampshire: 1.08%
#44: Maine: 1.05%
#45: North Dakota: 1.03%
#46: Utah: 0.96%
#47: Vermont: 0.88%
#48: Wyoming: 0.73%
#49: Idaho: 0.55%
#50: Montana: 0.43%
----------------------------------------------------------------------------------------------------
asians
#1: Hawaii: 38.24%
#2: California: 12.59%
#3: New York: 12.48%
#4: New Jersey: 7.91%
#5: Georgia: 7.04%
#6: Nevada: 6.76%
#7: Maryland: 5.23%
#8: Virginia: 5.14%
#9: Massachusetts: 5.10%
#10: Alaska: 4.98%
#11: Illinois: 4.44%
#12: Minnesota: 3.82%
#13: Connecticut: 3.63%
#14: Texas: 3.52%
#15: Oregon: 3.50%
#16: Washington: 3.38%
#17: Delaware: 3.08%
#18: Rhode Island: 2.95%
#19: Pennsylvania: 2.61%
#20: Colorado: 2.60%
#21: Arizona: 2.57%
#22: Michigan: 2.46%
#23: Florida: 2.33%
#24: Kansas: 2.30%
#25: Wisconsin: 2.14%
#26: New Hampshire: 2.11%
#27: North Carolina: 2.02%
#28: Utah: 1.88%
#29: Iowa: 1.64%
#30: Oklahoma: 1.62%
#31: Ohio: 1.62%
#32: Nebraska: 1.61%
#33: Missouri: 1.53%
#34: Indiana: 1.48%
#35: Louisiana: 1.47%
#36: Tennessee: 1.36%
#37: New Mexico: 1.32%
#38: Vermont: 1.18%
#39: South Carolina: 1.16%
#40: Arkansas: 1.13%
#41: Idaho: 1.13%
#42: Alabama: 1.07%
#43: Kentucky: 1.04%
#44: Maine: 1.01%
#45: North Dakota: 0.90%
#46: South Dakota: 0.86%
#47: Mississippi: 0.83%
#48: Wyoming: 0.73%
#49: West Virginia: 0.62%
#50: Montana: 0.61%