Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a geopandas geodataframe into a pandas dataframe

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?

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] )
like image 772
jberrio Avatar asked Mar 27 '18 05:03

jberrio


People also ask

Is GeoPandas the same as pandas?

GeoPandas is an open source project to make working with geospatial data in python easier. GeoPandas extends the datatypes used by pandas to allow spatial operations on geometric types. Geometric operations are performed by shapely. Geopandas further depends on fiona for file access and matplotlib for plotting.

What is a GeoPandas DataFrame?

A GeoDataFrame object is a pandas. DataFrame that has a column with geometry. In addition to the standard DataFrame constructor arguments, GeoDataFrame also accepts the following keyword arguments: Parameters.

How do I merge GeoPandas data frames?

There are two ways to combine datasets in geopandas – attribute joins and spatial joins. In an attribute join, a GeoSeries or GeoDataFrame is combined with a regular pandas Series or DataFrame based on a common variable. This is analogous to normal merging or joining in pandas.

Is GeoPandas part of pandas?

GeoPandas is based on the pandas. It extends pandas data types to include geometry columns and perform spatial operations. So, anyone familiar with pandas can easily adopt GeoPandas. The main data structure in GeoPandas is the GeoDataFrame that extends the pandas DataFrame.

How to convert a geodataframe to an array of values?

You don't need to convert the GeoDataFrame to an array of values, you can pass it directly to the DataFrame constructor: 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'):

What is a geodataframe in pandas?

A GeoDataFrame object is a pandas.DataFrame that has a column with geometry. In addition to the standard DataFrame constructor arguments, GeoDataFrame also accepts the following keyword arguments: Coordinate Reference System of the geometry objects.

How do I convert a pandas series to a Dataframe?

In this tutorial, you’ll see how to convert Pandas Series to a DataFrame. You’ll also observe how to convert multiple Series into a DataFrame. To begin, here is the syntax that you may use to convert your Series to a DataFrame: df = my_series.to_frame () Alternatively, you can use this approach to convert your Series: df = pd.DataFrame (my_series)

Is there a way to add geometry to a column in pandas?

A note - gdf.drop (columns='geometry') with the columns keyword only works since pandas version 0.21 which is relatively recent. It doesn't work for me and it may not work for others. Yes, that's true. The alternative is gdf.drop ('geometry', axis=1), will add that.


Video Answer


1 Answers

You don't need to convert the GeoDataFrame to an array of values, you can pass it directly to the DataFrame constructor:

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'):

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)
like image 99
joris Avatar answered Oct 13 '22 11:10

joris