将 geopandas 地理数据框转换为 Pandas 数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49504886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a geopandas geodataframe into a pandas dataframe
提问by jberrio
What is the most efficient way to convert a geopandas geodataframe into a pandas dataframe? Below is the method I use, is there another method which is more efficient or better in general at not generating errors?
将 geopandas 地理数据框转换为 Pandas 数据框的最有效方法是什么?下面是我使用的方法,是否有另一种方法在不产生错误方面更有效或更好?
import geopandas as gpd
import pandas as pd
# assuming I have a shapefile named shp1.shp
gdf1 = gpd.read_file('shp1.shp')
# then for the conversion, I drop the last column (geometry) and specify the column names for the new df
df1 = pd.DataFrame(gdf1.iloc[:,:-1].values, columns = list(gdf1.columns.values)[:-1] )
回答by joris
You don't need to convert the GeoDataFrame to an array of values, you can pass it directly to the DataFrame constructor:
您不需要将 GeoDataFrame 转换为值数组,您可以将其直接传递给 DataFrame 构造函数:
df1 = pd.DataFrame(gdf)
The above will keep the 'geometry' column, which is no problem for having it as a normal DataFrame. But if you actually want to drop that column, you can do (assuming the column is called 'geometry'):
以上将保留“几何”列,这对于将其作为普通 DataFrame 来说是没有问题的。但是,如果您确实想删除该列,则可以执行以下操作(假设该列称为“几何”):
df1 = pd.DataFrame(gdf.drop(columns='geometry'))
# for older versions of pandas (< 0.21), the drop part: gdf.drop('geometry', axis=1)
Two notes:
两个注意事项:
- It is often not needed to convert a GeoDataFrame to a normal DataFrame, because most methods that you know from a DataFrame will just work as well. Of course, there are a few cases where it is indeed needed (e.g. to plot the data without the geometries), and then the above method is the best way.
- The first way (
df1 = pd.DataFrame(gdf)
) will not take a copy of the data in the GeoDataFrame. This will often be good from an efficiency point of view, but depending on what you want to do with the DataFrame, you might want an actual copy:df1 = pd.DataFrame(gdf, copy=True)
- 通常不需要将 GeoDataFrame 转换为普通的 DataFrame,因为您从 DataFrame 中知道的大多数方法也可以正常工作。当然,在少数情况下确实需要它(例如绘制没有几何图形的数据),那么上述方法是最好的方法。
- 第一种方式 (
df1 = pd.DataFrame(gdf)
) 不会复制 GeoDataFrame 中的数据。从效率的角度来看,这通常是好的,但根据您想对 DataFrame 做什么,您可能需要一个实际的副本:df1 = pd.DataFrame(gdf, copy=True)