【问题标题】:Geting error: 'Int64Index' object has no attribute 'get_values'. What am I doing wrong?出现错误:“Int64Index”对象没有属性“get_values”。我究竟做错了什么?
【发布时间】:2021-02-22 23:10:48
【问题描述】:

我是数据科学的新手,我正在为这个项目而苦苦挣扎,我希望能得到一些帮助。所以,我正在使用一些 .shp 文件来获取一些具有其他功能的类似 choropleth 的地图,但由于我遇到了这个错误,我运气不好。

shp_path = '.../Comuna.shp'
sf = shp.Reader(shp_path)

   def read_shapefile(sf):
     fields = [x[0] for x in sf.fields][1:]
     records = sf.records()
     shps = [s.points for s in sf.shapes()]

df = pd.DataFrame(columns=fields, data=records)
df = df.assign(coords=shps)

return df

df = read_shapefile(sf)


REGION PROVINCIA COMUNA            NOM_REGION             NOM_PROVIN  \

0      13       131  13114  REGIÓN METROPOLITANA DE SANTIAGO    SANTIAGO   
1      13       131  13115  REGIÓN METROPOLITANA DE SANTIAGO    SANTIAGO  

 NOM_COMUNA                                             coords  
0            LAS CONDES  [(-70.47950849099993, -33.36433197899993), (-7...  
1          LO BARNECHEA  [(-70.32034044899996, -33.105245486999934), (-...  

df[df.NOM_COMUNA == 'SANTIAGO']

 REGION PROVINCIA COMUNA                        NOM_REGION NOM_PROVIN  \
25     13       131  13101  REGIÓN METROPOLITANA DE SANTIAGO   SANTIAGO   

   NOM_COMUNA                                             coords  
25   SANTIAGO  [(-70.66527655199997, -33.42827810699998), (-7...  

def plot_shape(id, s=None):
    """ PLOTS A SINGLE SHAPE """
    plt.figure()
    ax = plt.axes()
    ax.set_aspect('equal')
    shape_ex = sf.shape(id)
    x_lon = np.zeros((len(shape_ex.points),1))
    y_lat = np.zeros((len(shape_ex.points),1))
    for ip in range(len(shape_ex.points)):
        x_lon[ip] = shape_ex.points[ip][0]
        y_lat[ip] = shape_ex.points[ip][1]

    plt.plot(x_lon,y_lat) 
    x0 = np.mean(x_lon)
    y0 = np.mean(y_lat)
    plt.text(x0, y0, s, fontsize=10)
    # use bbox (bounding box) to set plot limits
    plt.xlim(shape_ex.bbox[0],shape_ex.bbox[2])
    return x0, y0

comuna = 'SANTIAGO' 
com_id =df[df.NOM_COMUNA == comuna].index.get_values()[0]
plot_shape(com_id, comuna)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-46-732395dd9018> in <module>
      1 comuna = 'SANTIAGO'
----> 2 com_id =df[df.NOM_COMUNA == comuna].index.get_values()[0]
      3 plot_shape(com_id, comuna)

AttributeError: 'Int64Index' object has no attribute 'get_values'

我已经构建了一些其他函数来获取此地图,但我认为问题的根源在于这个。 非常感谢。

【问题讨论】:

    标签: python pandas int64


    【解决方案1】:

    改用df[df.NOM_COMUNA == comuna].index.values

    您可以在其source code 中阅读更多关于 Int64Index 可以/不可以做什么的信息。

    【讨论】:

    • 可能也值得链接到pandas.index 的文档,因为它比完整的源代码更容易消化
    • 我确实打算包含 Int64Index 文档 - pandas.pydata.org/pandas-docs/stable/reference/api/…。该文档没有提供那么多信息 - 并且考虑索引本身,考虑索引的类型并了解它们如何分配给索引属性也可能导致混淆。如此公平,以至于我们需要更多文档。谢谢。
    • 当我使用你的答案时,我有这个错误类型错误:只有整数标量数组可以转换为标量索引。当我将其更改为 plot_shape(np.array(25), comuna) 时它起作用了来源:github.com/Mjrovai/Python4DS/blob/master/Vector_Mapping_Python/…
    • 我必须像这样添加第一个索引:com_id = df[df.NOM_COMUNA == comuna].index.values[0]
    猜你喜欢
    • 2022-01-25
    • 2022-06-10
    • 2014-06-15
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多