attributeError:' dataframe'对象没有属性' crs'当使用地理杂志时
使用地理器时,我要低于错误,而
AttributeError: 'DataFrame' object has no attribute 'crs'
在下面则是代码:
#geometry = [Point(xy) for xy in zip(complete_major_accidents['longitude'], complete_major_accidents['latitude'])]
#crs='none'
geometry = gpd.points_from_xy(complete_nonmajor_accidents.longitude, complete_nonmajor_accidents.latitude)
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
#geometries = world['geometry'].apply(lambda x: x.wkt).values
#print(geometries)
#print(tuple(geometry))
gdf = GeoDataFrame(complete_major_accidents, geometry)
gdf
ax = world[world['name'] == 'United Kingdom'].plot(figsize=(15, 15))
#print(type(ax))
gdf.plot(ax = ax, marker='o', color='red', markersize=15, edgecolor='black')
#gdf.plot(ax=world.plot(figsize=(15, 15)), marker='o', color='red', markersize=15)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/tmp/ipykernel_330/1106976374.py in <module>
12 ax = world[world['name'] == 'United Kingdom'].plot(figsize=(15, 15))
13 #print(type(ax))
---> 14 gdf.plot(ax = ax, marker='o', color='red', markersize=15, edgecolor='black')
15 #gdf.plot(ax=world.plot(figsize=(15, 15)), marker='o', color='red', markersize=15)
~/.local/lib/python3.8/site-packages/geopandas/plotting.py in __call__(self, *args, **kwargs)
961 kind = kwargs.pop("kind", "geo")
962 if kind == "geo":
--> 963 return plot_dataframe(data, *args, **kwargs)
964 if kind in self._pandas_kinds:
965 # Access pandas plots
~/.local/lib/python3.8/site-packages/geopandas/plotting.py in plot_dataframe(df, column, cmap, color, ax, cax, categorical, legend, scheme, k, vmin, vmax, markersize, figsize, legend_kwds, categories, classification_kwds, missing_kwds, aspect, **style_kwds)
674
675 if aspect == "auto":
--> 676 if df.crs and df.crs.is_geographic:
677 bounds = df.total_bounds
678 y_coord = np.mean([bounds[1], bounds[3]])
~/.local/lib/python3.8/site-packages/pandas/core/generic.py in __getattr__(self, name)
5573 ):
5574 return self[name]
-> 5575 return object.__getattribute__(self, name)
5576
5577 def __setattr__(self, name: str, value) -> None:
AttributeError: 'DataFrame' object has no attribute 'crs'
I am getting below error when using geopandas and shapely
AttributeError: 'DataFrame' object has no attribute 'crs'
Below is the code:
#geometry = [Point(xy) for xy in zip(complete_major_accidents['longitude'], complete_major_accidents['latitude'])]
#crs='none'
geometry = gpd.points_from_xy(complete_nonmajor_accidents.longitude, complete_nonmajor_accidents.latitude)
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
#geometries = world['geometry'].apply(lambda x: x.wkt).values
#print(geometries)
#print(tuple(geometry))
gdf = GeoDataFrame(complete_major_accidents, geometry)
gdf
ax = world[world['name'] == 'United Kingdom'].plot(figsize=(15, 15))
#print(type(ax))
gdf.plot(ax = ax, marker='o', color='red', markersize=15, edgecolor='black')
#gdf.plot(ax=world.plot(figsize=(15, 15)), marker='o', color='red', markersize=15)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/tmp/ipykernel_330/1106976374.py in <module>
12 ax = world[world['name'] == 'United Kingdom'].plot(figsize=(15, 15))
13 #print(type(ax))
---> 14 gdf.plot(ax = ax, marker='o', color='red', markersize=15, edgecolor='black')
15 #gdf.plot(ax=world.plot(figsize=(15, 15)), marker='o', color='red', markersize=15)
~/.local/lib/python3.8/site-packages/geopandas/plotting.py in __call__(self, *args, **kwargs)
961 kind = kwargs.pop("kind", "geo")
962 if kind == "geo":
--> 963 return plot_dataframe(data, *args, **kwargs)
964 if kind in self._pandas_kinds:
965 # Access pandas plots
~/.local/lib/python3.8/site-packages/geopandas/plotting.py in plot_dataframe(df, column, cmap, color, ax, cax, categorical, legend, scheme, k, vmin, vmax, markersize, figsize, legend_kwds, categories, classification_kwds, missing_kwds, aspect, **style_kwds)
674
675 if aspect == "auto":
--> 676 if df.crs and df.crs.is_geographic:
677 bounds = df.total_bounds
678 y_coord = np.mean([bounds[1], bounds[3]])
~/.local/lib/python3.8/site-packages/pandas/core/generic.py in __getattr__(self, name)
5573 ):
5574 return self[name]
-> 5575 return object.__getattribute__(self, name)
5576
5577 def __setattr__(self, name: str, value) -> None:
AttributeError: 'DataFrame' object has no attribute 'crs'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于能够通过将以下代码更改
为
I am finally able to resolve it by changing this below piece of code
to
从较旧版本更新地理杂志后,我遇到了同样的错误。后面的修复可以解决问题。
gpd.geodataframe()。
=
self.ax 18,12))
I got the same error after updating Geopandas from an older version. Following fix did the trick.
self.ax = gpd.GeoDataFrame().plot(figsize=(18, 12))
to
self.ax = gpd.GeoDataFrame(geometry=[]).plot(figsize=(18, 12))