From the course: Advanced Pandas

Explore Geographic data with Geopandas

From the course: Advanced Pandas

Start my 1-month free trial

Explore Geographic data with Geopandas

- [Instructor] Some of the most rewarding work I've been a part of has surrounded geospatial data. That is any data that has some locational component to it. It creates opportunities, both for analysis, and especially in data visualization to do something really cool. In this lesson, I want to introduce you to a sister package of Pandas called GeoPandas. As the name suggests, GeoPandas is an extension of Pandas that makes working with geospatial data simple and intuitive. To start us off, we'll import both Pandas and GeoPandas. Then create a dataframe that has a geospatial component. Here we're including latitude and longitude readings for five different peaks in Colorado. Now we're going to take this Panda's dataframe and convert it to a geodataframe, the GeoPandas equivalent. We pass in the dataframe, and point the geometry to the longitude and latitude readings. We're using a function called points_from_xy to interpret the latitude and longitude. Notice the output of our geodataframe. Geometry is represented as a point containing our coordinates. This is what makes a geodataframe, geo. Once you have your geometry configured, you can really begin to do the exciting work. For example, you could overlay a polygon and determine whether your points are inside or outside of it. And of course, now you can map these data points, which is what we'll do next. To map our peak data, we're going to use an excellent data visualization package called Plotly. Specifically we'll import Plotly Express, which is a lightweight and easy to use API that is light on the syntax. To visualize our data, we're going to create a Mapbox scatterplot. This requires an access token from Mapbox, which is completely free. When creating Plotly visuals, you want to create a figure, denoted as fig here. Which is essentially a dictionary passing our data and customizations in a way that Plotly can easily configure into a visual. The lat and lon in our figure are pointed to our freshly minted geometry field. And we're going to color by peak. Check it out. We can see each of our peaks here denoted by a circle. And if you hover over this circle you'll see a tool tip with detail. Visualizations in Plotly are interactive. Meaning you can move them around. You can hover over them, and you can filter as well. So we've turned our geodataframe into an interactive map. This only scratches the surface of what you can do with GeoPandas. Now, if your work doesn't involve geospatial data, I recommend you seek out some data sources and start to apply these techniques. It's a lot of fun.

Contents