【问题标题】:US Census API - Get The Population of Every City in a State Using PythonUS Census API - 使用 Python 获取州内每个城市的人口
【发布时间】:2015-11-23 16:24:19
【问题描述】:

我在获取特定州每个城市的人口时遇到了问题。我确实得到了城市的人口,但如果我将每个城市的人口相加,我得到的数字与该州的人口不同。

我的API Key 使用P0010001 variable 表示总人口 使用FIPS 25 表示马萨诸塞州,并通过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

我有理由确定“地方”是城市,例如

# 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"]]'

我做错了什么或误解了什么? 为什么各地方的人口总和不等于该州的人口?

List of example API calls

【问题讨论】:

  • 有些人住在城外……

标签: python python-3.x census


【解决方案1】:

如果我将每个城市的人口相加,我得到的数字与该州的人口不同。

这是因为不是每个人都住在城市中——在许多不属于任何城市的县都有农村“非法人地区”,而且人们确实住在那里。

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

【讨论】:

  • 所以这是一个 API 问题。您知道哪些地理参数会为这些未合并地区产生人口吗?
  • @Delicious,我相信你需要得到县流行然后减去县内城市的流行。至少,这就是我在census.gov/population/www/documentation/twps0082/twps0082.html 的字里行间读到的内容——但这不是一项全新的研究,所以据我所知,API 可能已经添加了你想要的功能(但如果有的话,我找不到在他们的文档中)。
  • 此功能今天不可用。甚至可用的数据也已过时(2010 年)
  • @BastienBastiens,2010 年是美国最新的人口普查,下一次计划在 2020 年进行,那么,2010 年的人口普查数据怎么“过时”了?!它在 2020 年之后的某个时间之前使用的官方值集,例如用于国会选区等目的。
  • @AlexMartelli 这些数据根据定义已经过时,因为它们来自 2010 年。我知道上一次人口普查是在 2010 年,但通过使用 2010 年和上一次人口普查。如果您的城市在 2000 年有 10 万居民,在 2010 年有 20 万居民,那么您的城市在 2015 年有大约 25 万居民。在上述情况下,2015 年的 25 万比使用最新人口普查的 20 万更准确。
【解决方案2】:

@Delicious -- 人口普查有多个可用的地理划分级别。我不能立即确定数据 API 停止在哪里(人口普查到各个区块,但我相信 API 不会,出于人类受试者的原因),但人口普查区、人口普查部门、ZCTA(邮政编码制表区 - 基本上是一个地图的邮政编码)都将涵盖地理范围,并包括县级的未合并人口。

您可以在人口普查数据网站(factfinder.census.gov --> Advanced Search)上使用这些不同的级别(以及绘图工具)。

【讨论】:

    猜你喜欢
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 2016-02-18
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    相关资源
    最近更新 更多