Python Tutorial: Introduction to GeoPandas

GeoPandas extends Pandas to handle spatial data like lines and polygons stored in GeoJSON, GeoPackage, or Shapefile formats. A GeoDataFrame loads with a geometry column that supports spatial methods like .area and .plot() for quick visualization. The library lets you work with geographic data the same way you'd work with regular Pandas tables.
Chapters
In this video, we will start introducing spatial specific Python libraries. In the last exercise, we used PANDAS to read a CSV file with the coordinates of points, and we used matplotlib to plot a map of those points. However, we also learned in the previous video that in addition to point data, spatial data can be made up of lines or polygons. Each object then consists of multiple points, and as such we will not be able to easily represent this in a CSV file or in a dataframe with two columns for the x and Y coordinates. Therefore, in the rest of the course we will use specific file formats for geospatial data, such as geojson files, geopackage files, or shape files, which are specialized in storing spatial data in addition to traditional tabular data. And to read such files and to work with such geospatial data in Python, we are going to use the geopandas library. Geopandas is a library for working with tabular geospatial vector data, extending the PANDAS dataframe. But let's start with importing some data. For this we can use the geopandas read file function through which we pass the path to the file. This function can read most of the occurring spatial vector formats. In this example, we are reading a geojson file with all the countries of the world. Using the head method to show the first five rows we you can see that we now have one column with the geometries, in this case polygons representing the countries and the other columns are attributes describing those countries. Let's quickly visualize the data so we can see that we indeed have all the countries of the world. For this we can use the plot method, which will make a basic visualization of of the geometries of the country's dataset. But what is this country's object? The readfile function of geopandas returned a geodataframe. You can think of it as a normal PANDAS dataframe supercharged with geospatial capabilities. We can use it to represent geospatial features with its attributes. It has always a geometry column that holds the geometry information, the features. The other columns are the attributes that describe each of the geometries. One of the specific aspects of a geodataframe is that it has a geometry attribute which always gives us back the geometry column regardless of its actual column name. For example, if we do this for the country's dataset, we see that we get a series with the polygons. What is returned here is a geoseries, just like a geodataframe is a geospatial equivalent of a PANDAS dataframe. The geoseries is like a PANDAS series but with additional spatial methods. One example of such geospatial functionality is is the area attribute of the geoseries. This attribute returns a series with the area of each geometry. To summarize, GeoPANDAS provides us the geodataframe and geoseries objects. They are just like their PANDAS counterparts and so all features you know of pandas still work. For example, we already used the head method to show the first rows, but in addition it has spatial abilities. We already have seen the customized plot method that plots the geometries and we have seen the geometry attribute which returns a geoseries. Further, it has a lot of spatial specific methods and attributes. We have already seen one example of this in the last slide, the area attribute, but you will see many more methods throughout the rest of this course. But enough talking, let's get you started with some actual geopanel.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







