美国人口普查 API - 使用 Python 获取州内每个城市的人口
US Census API - Get The Population of Every City in a State Using Python
我在获取特定州每个城市的人口时遇到了问题。我确实得到了城市的人口,但是如果我对每个城市的人口求和,我得到的数字与该州的人口不一样。
我得到了我的 API Key used the P0010001 variable for total population used the FIPS 25 for the state of Massachusetts and requested the population by the geography level "place",我理解它的意思是城市。
这是我使用的 Python 3 代码:
import urllib.request
import ast
class Census:
def __init__(self, key):
self.key = key
def get(self, fields, geo, year=2010, dataset='sf1'):
fields = [','.join(fields)]
base_url = 'http://api.census.gov/data/%s/%s?key=%s&get=' % (str(year), dataset, self.key)
query = fields
for item in geo:
query.append(item)
add_url = '&'.join(query)
url = base_url + add_url
print(url)
req = urllib.request.Request(url)
response = urllib.request.urlopen(req)
return response.read()
c = Census('<mykey>')
state = c.get(['P0010001'], ['for=state:25'])
# url: http://api.census.gov/data/2010/sf1?key=<mykey>&get=P0010001&for=state:25
county = c.get(['P0010001'], ['in=state:25', 'for=county:*'])
# url: http://api.census.gov/data/2010/sf1?key=<mykey>&get=P0010001&in=state:25&for=county:*
city = c.get(['P0010001'], ['in=state:25', 'for=place:*'])
# url: http://api.census.gov/data/2010/sf1?key=<mykey>&get=P0010001&in=state:25&for=place:*
# Cast result to list type
state_result = ast.literal_eval(state.decode('utf8'))
county_result = ast.literal_eval(county.decode('utf8'))
city_result = ast.literal_eval(city.decode('utf8'))
def count_pop_county():
count = 0
for item in county_result[1:]:
count += int(item[0])
return count
def count_pop_city():
count = 0
for item in city_result[1:]:
count += int(item[0])
return count
结果如下:
print(state)
# b'[["P0010001","state"],\n["6547629","25"]]'
print('Total state population:', state_result[1][0])
# Total state population: 6547629
print('Population in all counties', count_pop_county())
# Population in all counties 6547629
print('Population in all cities', count_pop_city())
# Population in all cities 4615402
我有理由确定 'place' 是城市,例如
# Get population of Boston (FIPS is 07000)
boston = c.get(['P0010001'], ['in=state:25', 'for=place:07000'])
print(boston)
# b'[["P0010001","state","place"],\n["617594","25","07000"]]'
我做错了什么或误解了什么? 为什么各地方的人口总和不等于各州的人口?
如果我对每个城市的人口求和,我得到的数字与该州的人口不一样。
那是因为不是每个人都住在城市 -- 在许多不属于任何城市的县里有农村"unincorporated areas",而且,人们确实住在那里.
所以,这不是编程问题!-)
@Delicious -- 人口普查有几个可用的地理划分级别。我不确定数据 API 在哪里停止(人口普查下降到各个街区,但我相信 API 不会,出于人类受试者的原因),但人口普查区,人口普查区,ZCTAs(邮政编码制表区域——基本上是地图的邮政编码)将全部涵盖地理范围,并包括县一级的未合并人口。
您可以在人口普查数据网站上玩这些不同的级别(以及地图工具):factfinder.census.gov --> 高级搜索。
我在获取特定州每个城市的人口时遇到了问题。我确实得到了城市的人口,但是如果我对每个城市的人口求和,我得到的数字与该州的人口不一样。
我得到了我的 API Key used the P0010001 variable for total population used the FIPS 25 for the state of Massachusetts and requested the population by the geography level "place",我理解它的意思是城市。
这是我使用的 Python 3 代码:
import urllib.request
import ast
class Census:
def __init__(self, key):
self.key = key
def get(self, fields, geo, year=2010, dataset='sf1'):
fields = [','.join(fields)]
base_url = 'http://api.census.gov/data/%s/%s?key=%s&get=' % (str(year), dataset, self.key)
query = fields
for item in geo:
query.append(item)
add_url = '&'.join(query)
url = base_url + add_url
print(url)
req = urllib.request.Request(url)
response = urllib.request.urlopen(req)
return response.read()
c = Census('<mykey>')
state = c.get(['P0010001'], ['for=state:25'])
# url: http://api.census.gov/data/2010/sf1?key=<mykey>&get=P0010001&for=state:25
county = c.get(['P0010001'], ['in=state:25', 'for=county:*'])
# url: http://api.census.gov/data/2010/sf1?key=<mykey>&get=P0010001&in=state:25&for=county:*
city = c.get(['P0010001'], ['in=state:25', 'for=place:*'])
# url: http://api.census.gov/data/2010/sf1?key=<mykey>&get=P0010001&in=state:25&for=place:*
# Cast result to list type
state_result = ast.literal_eval(state.decode('utf8'))
county_result = ast.literal_eval(county.decode('utf8'))
city_result = ast.literal_eval(city.decode('utf8'))
def count_pop_county():
count = 0
for item in county_result[1:]:
count += int(item[0])
return count
def count_pop_city():
count = 0
for item in city_result[1:]:
count += int(item[0])
return count
结果如下:
print(state)
# b'[["P0010001","state"],\n["6547629","25"]]'
print('Total state population:', state_result[1][0])
# Total state population: 6547629
print('Population in all counties', count_pop_county())
# Population in all counties 6547629
print('Population in all cities', count_pop_city())
# Population in all cities 4615402
我有理由确定 'place' 是城市,例如
# Get population of Boston (FIPS is 07000)
boston = c.get(['P0010001'], ['in=state:25', 'for=place:07000'])
print(boston)
# b'[["P0010001","state","place"],\n["617594","25","07000"]]'
我做错了什么或误解了什么? 为什么各地方的人口总和不等于各州的人口?
如果我对每个城市的人口求和,我得到的数字与该州的人口不一样。
那是因为不是每个人都住在城市 -- 在许多不属于任何城市的县里有农村"unincorporated areas",而且,人们确实住在那里.
所以,这不是编程问题!-)
@Delicious -- 人口普查有几个可用的地理划分级别。我不确定数据 API 在哪里停止(人口普查下降到各个街区,但我相信 API 不会,出于人类受试者的原因),但人口普查区,人口普查区,ZCTAs(邮政编码制表区域——基本上是地图的邮政编码)将全部涵盖地理范围,并包括县一级的未合并人口。
您可以在人口普查数据网站上玩这些不同的级别(以及地图工具):factfinder.census.gov --> 高级搜索。