跳转到主要内容

美国人口普查局的API包装器

项目描述

https://github.com/datamade/census/workflows/Python%20package/badge.svg

美国人口普查局API的简单包装器。

提供对ACS和SF1数据集的访问。

安装

pip install census

您可能还想安装一个互补库, us,它可以帮助您确定许多地理的 FIPS 代码。我们在下面的示例中使用它。

pip install us

用法

首先,获取您的 人口普查API密钥

from census import Census
from us import states

c = Census("MY_API_KEY")
c.acs5.get(('NAME', 'B25034_010E'),
          {'for': 'state:{}'.format(states.MD.fips)})

上述调用将返回马里兰州地理区域的名称以及1939年之前建造的房屋数量。已创建辅助方法以简化常见的几何调用

c.acs5.state(('NAME', 'B25034_010E'), states.MD.fips)

有关几何形状和states模块的完整详情请见下文。

get方法是ACS和SF1数据集上核心数据访问方法。第一个参数是单个字符串列或列的元组。第二个参数是具有for键和选项in键的geoemtry字典。for参数接受“*”通配符字符或Census.ALL。通配符对于in参数不有效。

默认情况下,数据集的年份是可用的最接近年份。要访问早期数据,将年份参数传递给API调用

c.acs5.state(('NAME', 'B25034_010E'), states.MD.fips, year=2010)

默认年份也可以在客户端范围内设置。

c = Census("MY_API_KEY", year=2010)

有关API的详细信息,请参阅人口普查数据API用户指南

数据集

对于每个数据集,列出的第一年是默认年份。

地理区域

API支持广泛的地理区域。这些区域的指定可能相当复杂,因此提供了一些便利方法。有关便利方法之外的更多地理区域,请参阅人口普查API文档

并非所有地理区域在所有年份都受支持。调用不支持该年份的便利方法将引发census.UnsupportedYearException。

地理关系文件提供在人口普查开发者网站上作为帮助用户比较1990年、2000年和2010年人口普查地理区域的工具。从这些文件中,数据用户可以确定一个普查中的地理区域与上一个普查中的地理区域之间的关系。

ACS5地理区域

  • state(fields, state_fips)

  • state_county(fields, state_fips, county_fips)

  • state_county_blockgroup(fields, state_fips, county_fips, blockgroup)

  • state_county_subdivision(fields, state_fips, county_fips, subdiv_fips)

  • state_county_tract(fields, state_fips, county_fips, tract)

  • state_place(fields, state_fips, place)

  • state_congressional_district(fields, state_fips, congressional_district)

  • state_legislative_district_upper(fields, state_fips, legislative_district)

  • state_legislative_district_lower(fields, state_fips, legislative_district)

  • us(fields)

  • state_zipcode(fields, state_fips, zip5)

ACS1地理区域

  • state(fields, state_fips)

  • state_congressional_district(fields, state_fips, district)

  • us(fields)

SF1地理区域

  • state(fields, state_fips)

  • state_county(fields, state_fips, county_fips)

  • state_county_subdivision(fields, state_fips, county_fips, subdiv_fips)

  • state_county_tract(fields, state_fips, county_fips, tract)

  • state_place(fields, state_fips, place)

  • state_congressional_district(fields, state_fips, district)

  • state_msa(fields, state_fips, msa)

  • state_csa(fields, state_fips, csa)

  • state_district_place(fields, state_fips, district, place)

  • state_zipcode(fields, state_fips, zip5)

PL地理区域

  • state(fields, state_fips)

  • state_county(fields, state_fips, county_fips)

  • state_county_subdivision(fields, state_fips, county_fips, subdiv_fips)

  • state_county_tract(fields, state_fips, county_fips, tract)

  • state_county_blockgroup(fields, state_fips, county_fips, blockgroup)

  • state_place(fields, state_fips, place)

  • state_congressional_district(fields, state_fips, district)

  • state_legislative_district_upper(fields, state_fips, legislative_district)

  • state_legislative_district_lower(fields, state_fips, legislative_district)

各州

此包之前有一个census.states模块,但现在使用us包。

>>> from us import states
>>> print states.MD.fips
u'24'

使用lookup()将FIPS转换为州简称

>>> print states.lookup('24').abbr
u'MD'

BYOS - 带自己的会话

如果您想使用自定义配置的requests.Session,可以将它传递给Census构造函数

s = requests.session()
s.headers.update({'User-Agent': 'census-demo/0.0'})

c = Census("MY_API_KEY", session=s)

您还可以替换特定数据集使用的会话

c.sf1.session = s

示例

阿拉斯加第170县所有人口普查区的地理名称

c.sf1.get('NAME', geo={'for': 'tract:*',
                       'in': 'state:{} county:170'.format(states.AK.fips)})

使用state_county_tract便利方法的相同调用

c.sf1.state_county_tract('NAME', states.AK.fips, '170', Census.ALL)

所有州的5-9岁男性总数

c.acs5.get('B01001_004E', {'for': 'state:*'})

使用州便利方法的相同调用

c.acs5.state('B01001_004E', Census.ALL)

不知道调查中的表格列表,试试这个

c.acs5.tables()

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分发

census-0.8.22.tar.gz (12.9 kB 查看哈希值)

上传时间

构建分发

census-0.8.22-py3-none-any.whl (11.3 kB 查看哈希值)

上传时间 Python 3

支持者