Hot answers tagged

123 votes
Accepted

Converting Pandas DataFrame to GeoDataFrame

Convert the DataFrame's content (e.g. Lat and Lon columns) into appropriate Shapely geometries first and then use them together with the original DataFrame to create a GeoDataFrame. from geopandas ...
user avatar
47 votes

Converting Pandas DataFrame to GeoDataFrame

Update 201912: The official documentation at https://geopandas.readthedocs.io/en/latest/gallery/create_geopandas_from_pandas.html does it succinctly using geopandas.points_from_xy like so: gdf = ...
user avatar
  • 1,493
47 votes

Finding nearest point in other GeoDataFrame using GeoPandas

If you have large dataframes, I've found that scipy's cKDTree spatial index .query method returns very fast results for nearest neighbor searches. As it uses a spatial index it's orders of magnitude ...
user avatar
  • 571
31 votes
Accepted

Finding nearest point in other GeoDataFrame using GeoPandas

You can directly use the Shapely function Nearest points (the geometries of the GeoSeries are Shapely geometries): from shapely.ops import nearest_points # unary union of the gpd2 geomtries pts3 = ...
user avatar
  • 51k
25 votes

Making shapefile from Pandas dataframe?

Yes, that can be done with shapely and geopandas. Supposed that your pandas dataframe kind of looks like this: import pandas as pd data = [ {'some_attribute': 'abc', 'lat': '50.1234', 'lon': '...
user avatar
  • 1,719
21 votes
Accepted

Reading raw data into geopandas

You can pass the json directly to the GeoDataFrame constructor: import geopandas as gpd import requests data = requests.get("https://data.cityofnewyork.us/api/geospatial/arq3-7z49?method=export&...
user avatar
  • 52.7k
14 votes
Accepted

Installing pandas with Esri's python?

I was able to get pandas working in ArcMap 10.1 using pandas packages already built for Anaconda. One of the nice things about Anaconda (besides being free) is that it's easy to create environments ...
user avatar
  • 2,232
13 votes
Accepted

GeoPandas to_file() saves GeoDataFrame without coordinate system

I have come across this behavior before. You need to explicitly pass the well known text (crs_wkt) string to the to_file() method. The string will then get passed to fiona.open(), which writes out ...
user avatar
  • 3,880
12 votes

Change marker size in plot with GeoPandas

In geopandas >= 0.3 (released September 2017), the plotting of points is based on the scatter plot method of matplotlib under the hood, and this accepts a variable markersize. So now you can actually ...
user avatar
  • 3,388
12 votes
Accepted

Quick way to get WKT from a GeoDataFrame's geometries in GeoPandas?

UPDATE: As of GeoPandas 0.9, there is a to_wkt method. mydf['wkt'] = mydf.geometry.to_wkt() Yes, there is to_wkt method in semi-private array module. I think it should be public in future, will have ...
user avatar
  • 2,139
12 votes
Accepted

Shapely coordinate sequence to GeoDataFrame

Make a shapely polygon geometry from coordinate pairs. Pass the polygon to GeoDataFrame constructor as a list. import geopandas as gpd from shapely.geometry import Polygon coords = [(151....
user avatar
11 votes
Accepted

Adding elevation, name and description in GeoJSON when converting DataFrame with Python

Using what i learned from the answer of @gene this solution avoids the use of iterrows because iterrows have performance issues. Valid for Python 3.X import pandas as pd import geojson def ...
user avatar
  • 293
10 votes
Accepted

TypeError: Input geometry column must contain valid geometry objects

I tested the following and works fine: from shapely import wkt import pandas as pd df = pd.DataFrame( {'name': ['testname', 'testname'], 'geom': ['MULTIPOLYGON (((21.78 40.36, ... 21.77 40....
user avatar
  • 465
10 votes
Accepted

Plotting a CSV with coordinates one row at a time via GeoPandas

The following script saves an image for each earthquake point in a single plot. from shapely.geometry import Point from geopandas import GeoDataFrame import geopandas as gpd import pandas as pd ...
user avatar
8 votes

Assign a point to polygon using pandas and shapely

If you're working with spatial data and Pandas you should take a look at GeoPandas. The example below demonstrates how to perform a spatial join in GeoPandas (which uses Shapely). A GeoDataFrame ...
user avatar
8 votes

Finding nearest point in other GeoDataFrame using GeoPandas

Figured it out: def min_dist(point, gpd2): gpd2['Dist'] = gpd2.apply(lambda row: point.distance(row.geometry),axis=1) geoseries = gpd2.iloc[gpd2['Dist'].argmin()] return geoseries Of ...
user avatar
  • 1,211
8 votes
Accepted

"Copy" Image coordinates to another image that is nd.array

Save result array (rf_array) as in the following lines: rf_array = df_join['Prediction'].values # returns numpy.ndarray rf_array = rf_array.reshape(img.shape[0], img.shape[1]) # rf_array = rf_array....
user avatar
7 votes

Making shapefile from Pandas dataframe?

For ArcMap you need to define the Projection before exporting to Shapefile. import geopandas df = geopandas.GeoDataFrame(df, geometry='geometry') # proj WGS84 df.crs= "+proj=longlat +ellps=WGS84 +...
user avatar
7 votes
Accepted

GeoPandas GeoDataFrame plot statistics - how?

The GeoPandas plotting methods are there for convenience. They do override the standard pandas plotting methods. For now, the easiest way to get access to basic pandas plotting is probably through ...
user avatar
  • 331
7 votes

ImportError: No module named pandas in Python window of ArcMap after installing with pip?

Pandas requires compilation. If you don't have an appropriate compiler and build dependencies installed, you'll need to install a precompiled version. Pandas depends on numpy. The Numpy application ...
user avatar
  • 52.7k
7 votes
Accepted

Merging a geodataframe and pandas dataframe based on a column

You should specify a common key using on parameter. I removed merged_ prefix for legibility. df = spatial_df.merge(tab_df, on='mukey', how='left') # df = tab_df.merge(spatial_df, on='mukey', how='...
user avatar
7 votes
Accepted

Pandas DataFrame to GeoDataFrame with Polygon geometry using groupby and lambda

Instead of using apply this can be done using the agg method with named aggregations. The only thing is that agg cannot yet operate on multiple columns, so the points must be condensed to a single ...
user avatar
  • 884
6 votes
Accepted

NumPyArrayToTable - RuntimeError: unsupported time units. use M8[us]

Playing around with dates using numpy, pandas, and arcpy tools for writing numpy arrays into geodatabase tables can be challenging. I usually use arcpy.da.InsertCursor in favor of arcpy.da....
user avatar
6 votes

Geopandas: Write layer back into GeoDataBase

As with Fiona where you can specify the driver when you save a layer, you can do the same thing with GeoPandas (Writing Spatial Data) GeoDataFrames can be exported to many different standard ...
user avatar
  • 51k
6 votes
Accepted

US Cities (from JSON) and USA––Same CRS, Wrong Projection

The problem appears to be here: geometry = [Point(xy) for xy in zip(cities_df['latitude'], cities_df['longitude'])] Latitude is y, longitude is x. Just swap the keys. You can see the transposition ...
user avatar
  • 3,897
6 votes
Accepted

Iterate over ImageCollection returning pandas dataframe using earth engine & python

Found a solution, by making a list of the scene IDs from the imageCollection and iterating over the list. Then in a loop I import the individual images instead of mapping/iterating over the ...
user avatar
  • 367
6 votes
Accepted

Groupby Ploting - give each plot title/name

If you want to represent the continents side by side, you have to create subplots on the same fig. To do so, you can iterate through your group of continents. Here is an example: import geopandas as ...
user avatar
  • 322
6 votes
Accepted

Merging two datasets where polygons are nearly identical using geopandas?

Consider the following example dataframes: import geopandas from shapely.geometry import Polygon df1 = geopandas.GeoDataFrame( {'geometry': [Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]), ...
user avatar
  • 3,388
6 votes
Accepted

How to process 2 GeoDataFrames by rows pairwise?

If the two GeoDataFrames are "aligned" (same length with same index), you can actually directly use the distance method: this accepts another GeoSeries or GeoDataFrame, and then the distance will be ...
user avatar
  • 3,388
6 votes
Accepted

Creating QgsVectorLayer from Pandas dataframe

Your method is the simplest. However, there is an alternative by creating a new layer in QGIS and completing it by browsing your dataframe pandas. import pandas as pd # Declaration of my pandas ...
user avatar
  • 3,855

Only top scored, non community-wiki answers of a minimum length are eligible