diff options
author | Christian Cleberg <hello@cleberg.net> | 2024-04-22 14:07:21 -0500 |
---|---|---|
committer | Christian Cleberg <hello@cleberg.net> | 2024-04-22 14:07:21 -0500 |
commit | 3def68d80edf87e28473609c31970507d9f03467 (patch) | |
tree | a64fb6363727dbfba4125d1b3c9d5c1423019b5e /content/blog/2020-09-25-happiness-map.org | |
parent | 9ad1dcee850864fd2c8564ac90e4154ce68ae2b8 (diff) | |
download | cleberg.net-3def68d80edf87e28473609c31970507d9f03467.tar.gz cleberg.net-3def68d80edf87e28473609c31970507d9f03467.tar.bz2 cleberg.net-3def68d80edf87e28473609c31970507d9f03467.zip |
format a portion of blog posts
Diffstat (limited to 'content/blog/2020-09-25-happiness-map.org')
-rw-r--r-- | content/blog/2020-09-25-happiness-map.org | 73 |
1 files changed, 34 insertions, 39 deletions
diff --git a/content/blog/2020-09-25-happiness-map.org b/content/blog/2020-09-25-happiness-map.org index 1eab63e..1f2b56f 100644 --- a/content/blog/2020-09-25-happiness-map.org +++ b/content/blog/2020-09-25-happiness-map.org @@ -4,10 +4,9 @@ #+filetags: :data: * Background Information -The dataset (obtained from -[[https://www.kaggle.com/unsdsn/world-happiness][Kaggle]]) used in this -article contains a list of countries around the world, their happiness -rankings and scores, as well as other national scoring measures. +The dataset (obtained from [[https://www.kaggle.com/unsdsn/world-happiness][Kaggle]]) 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: @@ -20,8 +19,8 @@ Fields include: - 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 @@ -32,9 +31,8 @@ analysis. #+end_src * Import the Data -We only need a couple packages to create a choropleth map. We will use -[[https://python-visualization.github.io/folium/][Folium]], which -provides map visualizations in Python. We will also use geopandas and +We only need a couple packages to create a choropleth map. We will use [[https://python-visualization.github.io/folium/][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. #+begin_src python @@ -44,14 +42,14 @@ import geopandas as gpd import pandas as pd #+end_src -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: #+begin_src python # Load the GeoJSON data with geopandas @@ -62,9 +60,9 @@ geo_data.head() #+caption: GeoJSON Dataframe [[https://img.cleberg.net/blog/20200925-world-choropleth-map/geojson_df.png]] -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: #+begin_src python # Load the world happiness data with pandas @@ -76,12 +74,11 @@ happy_data.head() [[https://img.cleberg.net/blog/20200925-world-choropleth-map/happiness_df.png]] * 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. #+begin_src python # Rename some countries to match our GeoJSON data @@ -105,11 +102,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. #+begin_src python # Merge the two previous dataframes into a single geopandas dataframe @@ -125,10 +122,9 @@ merged_df = merged_df.rename(columns = {'Country or region':'Country'}) * 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. #+begin_src python # Assign centroids to map @@ -162,10 +158,9 @@ Let's look at the resulting map. [[https://img.cleberg.net/blog/20200925-world-choropleth-map/map.png]] * 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. #+begin_src python # Adding labels to map @@ -210,8 +205,8 @@ folium.LayerControl().add_to(world_map) world_map #+end_src -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. #+caption: Choropleth Map Tooltip [[https://img.cleberg.net/blog/20200925-world-choropleth-map/tooltip_map.png]] |