首页 > 解决方案 > Cenpy 库无法获取宾夕法尼亚州匹兹堡的数据

问题描述

我正在做一个项目,我试图分析来自美国宾夕法尼亚州匹兹堡的大量 ACS 人口普查数据。我可以轻松地访问 data.census.gov 来获取我正在寻找的 138 个人口普查区域所需的数据,但这根本没有效率。所以我下载了 cenpy 库,它对纽约市的 ACS 数据非常有用。这是纽约市的一个例子:

NYC_income = products.ACS(2019).from_place('New York City, NY', level = 'tract',
                                          variables = ['B19013_001E'])

这很好用,会给我一个带有我传入的 ACS 变量的地理数据框。我已经在匹兹堡尝试过,但它不起作用,错误也不是很有帮助:

pgh_Test = products.ACS(2019).from_place('Pittsburgh, PA', level='tract', 
                                        variables = ['B01001A_001E'])

这将返回一个错误:

KeyError: 'Response from API is malformed. You may have submitted too many queries, 
formatted the request incorrectly, or experienced significant network connectivity 
issues. Check to make sure that your inputs, like placenames, are spelled correctly, 
and that your geographies match the level at which you intend to query. The original 
error from the Census is:\\n(API ERROR 400:Failed to execute query.([]))'

我还尝试了其他拼写Pittsburgh方式,例如Pittsburgh City, Pittsburgh city, Pittsburg, 并且还尝试拼写状态而不是使用首字母缩写词。

最后,我很好奇是否有人遇到过这个问题以及如何解决它,以便我可以通过 cenpy 访问匹兹堡 ACS 数据,而不是通过 data.census.gov 选择每个人口普查区。

先感谢您!

标签: pythoncensus

解决方案


'County Subdivision'用作place_type. _ 似乎有助于正确解决该位置:

products.ACS(2019).from_place('Pittsburgh, PA',
                              place_type='County Subdivision',
                              level='tract',
                              variables = ['B01001A_001E'])

输出:

Matched: Pittsburgh, PA to Pittsburgh city within layer County Subdivisions
GEOID   geometry    B01001A_001E    state   county  tract
0   42003270300 POLYGON ((-8910344.550 4935795.800, -8910341.7...   1154.0  42  003 270300
1   42003980600 POLYGON ((-8909715.600 4933176.800, -8909606.6...   13.0    42  003 980600
2   42003051100 POLYGON ((-8903296.360 4930484.040, -8903251.9...   0.0 42  003 051100
3   42003050900 POLYGON ((-8903766.910 4931335.660, -8903642.5...   40.0    42  003 050900
4   42003562000 POLYGON ((-8901104.700 4930705.200, -8901104.1...   1826.0  42  003 562000
... ... ... ... ... ... ...
84  42003980500 POLYGON ((-8899981.160 4929217.570, -8899977.7...   16.0    42  003 980500
85  42003140200 POLYGON ((-8898569.740 4931230.040, -8898532.8...   1932.0  42  003 140200
86  42003111300 POLYGON ((-8898077.150 4934571.530, -8898053.1...   1499.0  42  003 111300
87  42003111500 POLYGON ((-8898240.670 4932660.630, -8898229.9...   942.0   42  003 111500
88  42003120700 POLYGON ((-8895502.550 4932516.230, -8895493.0...   17.0    42  003 120700
89 rows × 6 columns

此参数的其他值是'Incorporated Place''Census Designated Place'。从文档中

地方类型:str

要关注的地点类型、法人地点、县分区或人口普查指定地点。

在这个 colab中查看演示。


推荐阅读