Reputation: 21
I'm kinda new to Data Science and I'm struggling with this project, I would appreciate some help. So, I'm working with some .shp files to get some choropleth-like maps with some other functions, that for my luck are not working because of this error I'm getting.
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'
I've built some other functions to get this maps, but I think the root of the problem is this one. Thanks a lot.
Upvotes: 2
Views: 4206
Reputation: 3538
Try df[df.NOM_COMUNA == comuna].index.values
instead.
You can read more about what Int64Index can/cannot do here in its source code.
Upvotes: 3