Cenpy Library无法获取宾夕法尼亚州匹兹堡市的数据

2024-05-16 14:37:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

这很好,将给我一个带有我传入的ACS变量的geodataframe。我在匹兹堡试过这个方法,但不起作用,错误也没有多大帮助:

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 CityPittsburgh cityPittsburg,还尝试了拼写状态而不是使用首字母缩略词

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

提前谢谢你


Tags: theto数据fromcitydataplacelevel
1条回答
网友
1楼 · 发布于 2024-05-16 14:37:45

使用'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'。从the documentation

place_type : str

type of place to focus on, Incorporated Place, County Subdivision, or Census Designated Place.

请参阅this colab中的演示

相关问题 更多 >