我正在try 使用Plotly Express创建交互的人口普查数据集合,我使用censusdis包检索这些数据.这适用于我检索的两个变量,但不适用于第三个变量.以下是我演示该问题的代码:

import plotly.express as px
import censusdis.data as ced
from censusdis.datasets import ACS5

#variable = 'B19013_001E' # Works - Median Household Income
#variable = 'B25058_001E' # Works - Median Rent
variable = 'B01001_001E' # Does not work! Total Population

df = ced.download(
    dataset=ACS5,
    vintage=2022,         
    download_variables=['NAME', variable], 
    state='06',
    county='075',
    tract='*',
    with_geometry=True)

df = df.set_index('NAME')

print(df.head())

fig = px.choropleth_mapbox(df, 
                           geojson=df.geometry,
                           locations=df.index, 
                           center={'lat': 37.74180915, 'lon': -122.38474831884692}, 
                           color=variable, 
                           color_continuous_scale="Viridis", 
                           mapbox_style="carto-positron", 
                           opacity=0.5,
                           zoom=10)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

As I cycle through the variables, the resulting dataframes all appear similar, but the third one (B01001_001E) generates a scale but not a map:enter image description here

但是,GEOMETRY列看起来很好(实际上,它看起来与为其他变量返回的列相同).如果您能帮助我了解问题所在,并就如何解决问题提供建议,我将不胜感激.

推荐答案

ced.download的返回值中,有一些遗漏的值.你可以和他们一起看

print(df[df.isna().any(axis='columns')])

它产生了

                                                   STATE COUNTY   TRACT  B01001_001E geometry
NAME                                                                                         
Census Tract 9901; San Francisco County; Califo...    06    075  990100            0     None
Census Tract 9902; San Francisco County; Califo...    06    075  990200            0     None

如果你处理掉那些

df = df.dropna()

那么它就运行得很好.

Python相关问答推荐

2维数组9x9,不使用numpy.数组(MutableSequence的子类)

根据条件将新值添加到下面的行或下面新创建的行中

使用新的类型语法正确注释ParamSecdecorator (3.12)

try 在树叶 map 上应用覆盖磁贴

使用miniconda创建环境的问题

. str.替换pandas.series的方法未按预期工作

删除所有列值,但判断是否存在任何二元组

将输入管道传输到正在运行的Python脚本中

管道冻结和管道卸载

Mistral模型为不同的输入文本生成相同的嵌入

如何禁用FastAPI应用程序的Swagger UI autodoc中的application/json?

Pandas:计算中间时间条目的总时间增量

在matplotlib中使用不同大小的标记顶部添加批注

如何过滤组s最大和最小行使用`transform`'

Python Mercury离线安装

如何提高Pandas DataFrame中随机列 Select 和分配的效率?

将数字数组添加到Pandas DataFrame的单元格依赖于初始化

.awk文件可以使用子进程执行吗?

使用pythonminidom过滤XML文件

Numpy`astype(Int)`给出`np.int64`而不是`int`-怎么办?