美国人口普查API - 使用Python获取一个州中每个城市的人口数量

6
我遇到了一个问题,无法获取特定州每个城市的人口数量。我可以获取每个城市的人口数量,但如果将每个城市的人口总和起来,得到的结果与该州的人口总数不同。
我使用了API密钥并使用了P0010001变量作为总人口数,在请求马萨诸塞州(FIPS 25)所在的州的人口时,按地理级别“地方”获取人口,这意味着城市。
以下是我使用的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"]]'

我做错了什么或者有什么误解?为什么按地点统计的人口总和不等于州的人口总数? 示例API调用列表

2
有些人生活在城市之外... - Benjamin
2个回答

8

如果我把每个城市的人口加起来,得到的数字与该州的总人口不同。

这是因为并非所有人都住在城市里 - 许多县有农村“未划分区域”,这些地区不属于任何城市,但确实有人居住。

所以,这不是一个编程问题!-)


1
所以这是一个API问题。你知道哪个地理参数会产生这些未合并区域的人口吗? - Delicious
1
@Delicious,我认为你需要获取县的人口,然后减去县内城市的人口。至少,这是我在https://www.census.gov/population/www/documentation/twps0082/twps0082.html中读到的信息 - 但这不是一项全新的研究,所以我不知道API是否已经添加了您需要的功能(但如果他们有,我在他们的文档中找不到)。 - Alex Martelli
今天还没有这种能力。即使可用的数据也已过时(2010年)。 - Bastien Bastiens
@BastienBastiens,2010年是最近一次美国人口普查,下一次将于2020年进行,那么,2010年的人口普查数据怎么会“过时”呢?!它 官方使用的数值集合,例如用于国会选区划分等目的,直到2020年之后的某个时间。 - Alex Martelli
@AlexMartelli 数据因为来自2010年而被定义为过时的。我知道最近一次人口普查是在2010年,但是通过使用2010年和上一次人口普查的数据进行推断,创建一个更准确的估计将非常容易。如果你的城市在2000年有10万居民,在2010年有20万居民,那么你的城市在2015年大约有25万居民。在上述情况中,使用2015年的25万居民比使用最新人口普查的20万居民更准确。 - Bastien Bastiens

1
@Delicious -- 人口普查提供了几个层次的地理划分。我不确定数据API的截止位置(人口普查可以到达单个街区,但出于人类研究的原因,我认为API不能这样做),但是人口普查区、人口普查区划分、ZCTA(邮政编码制表区--基本上是地图上的邮政编码)都涵盖了地理范围,并包括县级以下未纳入城镇管理的人口。您可以在人口普查数据网站factfinder.census.gov上玩弄这些不同的层次(以及映射工具)-->高级搜索。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接