choropleth_geojson TypeError:描述符“lower”需要一个“str”对象,但收到一个“int”

2024-05-15 13:45:12 发布

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

我正在尝试使用choropleth_geojson将geojson文件与choropleth映射中的csv数据结合起来,如minimal_example.py示例:

import choropleth_geojson as cg
import pandas as pd
import json
import plotly.offline as offline

mapbox_access_token = 'key'

df = pd.read_csv('indicadores_ma.csv',usecols=['geocod','Impacto (2015)'],sep=';')

with open(r'county_ma.geojson') as f:
    geojson = json.load(f)

northamerica = cg.choropleth(mapbox_access_token, df, geojson, 'geocod')
fig = northamerica.choroplot()

offline.plot(fig, auto_open=True)

输入数据文件包括:

**indicators_ma.csv

enter image description here

县/马州地理JSON

{
    "type": "FeatureCollection",
    "features": [{
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon",
                "coordinates": [[[[-42.815, -5.3142],...]]]]
            },
            "properties": {
                "geocod": 2112209,
                "name": "Timon/MA"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon",
                "coordinates": [[[[-41.8141, -2.7393],...]]]
            },
            "properties": {
                "geocod": 2100907,
                "name": "Araioses/MA"
            }
        }
    ]
}

fig = northamerica.choroplot() 

我收到了错误信息:

File "pandas/_libs/lib.pyx", line 2217, in pandas._libs.lib.map_infer

TypeError: descriptor 'lower' requires a 'str' object but received a 'int'

我做错了什么

此消息中使用的文件可以从here下载


Tags: 文件csvimportpandasasgeojsontypefig
1条回答
网友
1楼 · 发布于 2024-05-15 13:45:12

我找到了解决办法。在示例数据中,csv文件的第一列没有名称。它假设第一列应该用作索引列,它是一个字符串

在我的数据中,我指定了所有列名,因此pandas创建了一个整数索引列,当choropleth_geojson尝试将索引值转换为小写时,该列会生成错误消息。解决方案是在dead_csv中指定索引列:

df = pd.read_csv('indicadores.csv',index_col=[0],usecols=['geocod','Impacto (2015)'],sep=';')

相关问题 更多 >