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

时间:2019-10-22 18:18:01

标签: python geojson choropleth

我正在尝试使用choropleth_geojson在choropleth映射中将geojson文件与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)

输入数据文件为:

** indicadores_ma.csv

enter image description here

county_ma.geojson

{
    "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下载。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。在示例数据中,csv文件的第一列没有名称。它使大熊猫假设第一列应该用作索引列,并且它是一个字符串。

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

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