Clean Up your MESSY MAPS in Python
Table of Contents
· Introduction
· Specifying Certain Columns
· Displaying the Updated Map
· Renaming the Columns
· Displaying the Updated Map WITH THE RENAMED COLUMNS
· Video Explanation
Introduction
In the map above, there are too many columns showing up when I hover over one of the polygons. In this article, you’ll learn how to remove the columns that you don’t want and keep the ones that you do want in the dataframe.
This is a part 2 article; if you haven’t already, check out my part 1 article, where I show you step-by-step how to create the map!
Specifying Certain Columns
Let’s say that I want to only see 3 of the columns when I hover over the polygons: “community”, “Pop2014”, “Property_C”. To do this, I can do this:
I’m keeping the “geometry” column because I need the coordinates to be able to create a map from the data.
This will get a dataframe called “file” that only shows the columns that we want:
Displaying the Updated Map
Now we can display the map again on the property crime rates in Chicago:
And if I hover over the polygon, it’s a lot cleaner:
Renaming the Columns
However, there’s still a problem.
If you haven’t looked at the dataset description, you might not know what “Pop2014” or “Property_C” means. I’m going to change the column names so that they make more sense to everyone.
Here’s the code for this:
Inside the .rename method, you can see that I passed in a dictionary. Each key in the dictionary represents the ORIGINAL column name, and each value represents the NEW column name for its corresponding key.
So, in this example, I change “community” to “Neighborhood”; “Pop2014” to “Population in 2014”; and “Property_C” to “Property Crime”.
Here’s what the dataframe should look like now:
Displaying the Updated Map WITH THE RENAMED COLUMNS
Now, let’s display the map with our updated dataframe:
Notice that I said “Property Crime” instead of “Property_C”. This is because we changed the column, so “Property_C” no longer exists. If you tried to pass in “Property_C”, you would get an error.
Looking at the map, all of our 3 columns are changed and that looks really clean!
Video Explanation
Thank you for making it all the way here! I’ve made a YouTube tutorial that goes over the steps that I talked about in this post. Feel free to check that out here:
Originally published at https://dylans0ng.github.io on August 3, 2023.