diff options
author | Christian Cleberg <hello@cleberg.net> | 2024-04-29 14:18:55 -0500 |
---|---|---|
committer | Christian Cleberg <hello@cleberg.net> | 2024-04-29 14:18:55 -0500 |
commit | fdd80eadcc2f147d0198d94b7b908764778184a2 (patch) | |
tree | fbec9522ea9aa13e8105efc413d2498c3c5b4cd6 /content/blog/2020-09-25-happiness-map.md | |
parent | d6c80fdc1dea9ff242a4d3c7d3939d2727a8da56 (diff) | |
download | cleberg.net-fdd80eadcc2f147d0198d94b7b908764778184a2.tar.gz cleberg.net-fdd80eadcc2f147d0198d94b7b908764778184a2.tar.bz2 cleberg.net-fdd80eadcc2f147d0198d94b7b908764778184a2.zip |
format line wrapping and fix escaped characters
Diffstat (limited to 'content/blog/2020-09-25-happiness-map.md')
-rw-r--r-- | content/blog/2020-09-25-happiness-map.md | 95 |
1 files changed, 46 insertions, 49 deletions
diff --git a/content/blog/2020-09-25-happiness-map.md b/content/blog/2020-09-25-happiness-map.md index a9ba336..77d9c55 100644 --- a/content/blog/2020-09-25-happiness-map.md +++ b/content/blog/2020-09-25-happiness-map.md @@ -8,28 +8,28 @@ draft = false # Background Information The dataset (obtained from -[Kaggle](https://www.kaggle.com/unsdsn/world-happiness)) used in this -article contains a list of countries around the world, their happiness -rankings and scores, as well as other national scoring measures. +[Kaggle](https://www.kaggle.com/unsdsn/world-happiness)) used in this article +contains a list of countries around the world, their happiness rankings and +scores, as well as other national scoring measures. Fields include: -- Overall rank -- Country or region -- GDP per capita -- Social support -- Healthy life expectancy -- Freedom to make life choices -- Generosity -- Perceptions of corruption +- Overall rank +- Country or region +- GDP per capita +- Social support +- Healthy life expectancy +- Freedom to make life choices +- Generosity +- Perceptions of corruption -There are 156 records. Since there are \~195 countries in the world, we -can see that around 40 countries will be missing from this dataset. +There are 156 records. Since there are ~195 countries in the world, we can see +that around 40 countries will be missing from this dataset. # Install Packages -As always, run the `install` command for all packages needed -to perform analysis. +As always, run the `install` command for all packages needed to perform +analysis. ``` python !pip install folium geopandas matplotlib numpy pandas @@ -38,9 +38,9 @@ to perform analysis. # Import the Data We only need a couple packages to create a choropleth map. We will use -[Folium](https://python-visualization.github.io/folium/), which provides -map visualizations in Python. We will also use geopandas and pandas to -wrangle our data before we put it on a map. +[Folium](https://python-visualization.github.io/folium/), which provides map +visualizations in Python. We will also use geopandas and pandas to wrangle our +data before we put it on a map. ``` python # Import the necessary Python packages @@ -49,14 +49,14 @@ import geopandas as gpd import pandas as pd ``` -To get anything to show up on a map, we need a file that will specify -the boundaries of each country. Luckily, GeoJSON files exist (for free!) -on the internet. To get the boundaries of every country in the world, we -will use the GeoJSON link shown below. +To get anything to show up on a map, we need a file that will specify the +boundaries of each country. Luckily, GeoJSON files exist (for free!) on the +internet. To get the boundaries of every country in the world, we will use the +GeoJSON link shown below. -GeoPandas will take this data and load it into a dataframe so that we -can easily match it to the data we\'re trying to analyze. Let\'s look at -the GeoJSON dataframe: +GeoPandas will take this data and load it into a dataframe so that we can easily +match it to the data we're trying to analyze. Let's look at the GeoJSON +dataframe: ``` python # Load the GeoJSON data with geopandas @@ -67,9 +67,9 @@ geo_data.head()  -Next, let\'s load the data from the Kaggle dataset. I\'ve downloaded -this file, so update the file path if you have it somewhere else. After -loading, let\'s take a look at this dataframe: +Next, let's load the data from the Kaggle dataset. I've downloaded this file, +so update the file path if you have it somewhere else. After loading, let's +take a look at this dataframe: ``` python # Load the world happiness data with pandas @@ -82,12 +82,11 @@ Dataframe](https://img.cleberg.net/blog/20200925-world-choropleth-map/happiness_ # Clean the Data -Some countries need to be renamed, or they will be lost when you merge -the happiness and GeoJSON dataframes. This is something I discovered -when the map below showed empty countries. I searched both data frames -for the missing countries to see the naming differences. Any countries -that do not have records in the `happy_data` df will not show -up on the map. +Some countries need to be renamed, or they will be lost when you merge the +happiness and GeoJSON dataframes. This is something I discovered when the map +below showed empty countries. I searched both data frames for the missing +countries to see the naming differences. Any countries that do not have records +in the `happy_data` df will not show up on the map. ``` python # Rename some countries to match our GeoJSON data @@ -112,11 +111,11 @@ happy_data.at[democratic_congo_index, 'Country or region'] = 'Democratic Republi # Merge the Data Now that we have clean data, we need to merge the GeoJSON data with the -happiness data. Since we\'ve stored them both in dataframes, we just -need to call the `.merge()` function. +happiness data. Since we've stored them both in dataframes, we just need to +call the `.merge()` function. -We will also rename a couple columns, just so that they\'re a little -easier to use when we create the map. +We will also rename a couple columns, just so that they're a little easier to +use when we create the map. ``` python # Merge the two previous dataframes into a single geopandas dataframe @@ -133,10 +132,9 @@ Dataframe](https://img.cleberg.net/blog/20200925-world-choropleth-map/merged_df. # Create the Map The data is finally ready to be added to a map. The code below shows the -simplest way to find the center of the map and create a Folium map -object. The important part is to remember to reference the merged -dataframe for our GeoJSON data and value data. The columns specify which -geo data and value data to use. +simplest way to find the center of the map and create a Folium map object. The +important part is to remember to reference the merged dataframe for our GeoJSON +data and value data. The columns specify which geo data and value data to use. ``` python # Assign centroids to map @@ -164,17 +162,16 @@ folium.Choropleth( ).add_to(world_map) ``` -Let\'s look at the resulting map. +Let's look at the resulting map.  # Create a Tooltip on Hover -Now that we have a map set up, we could stop. However, I want to add a -tooltip so that I can see more information about each country. The -`tooltip_data` code below will show a popup on hover with all -the data fields shown. +Now that we have a map set up, we could stop. However, I want to add a tooltip +so that I can see more information about each country. The `tooltip_data` code +below will show a popup on hover with all the data fields shown. ``` python # Adding labels to map @@ -219,8 +216,8 @@ folium.LayerControl().add_to(world_map) world_map ``` -The final image below will show you what the tooltip looks like whenever -you hover over a country. +The final image below will show you what the tooltip looks like whenever you +hover over a country.  |