SQL & Seaborn: Can Money Buy Happiness?
By: Amanda West
Tags: Sql, Jupyter Notebook, Programming, Python, Data Science

SQL & Seaborn: Can Money Buy Happiness?

Beginner’s Guide to Merging and Graphing Data

Once in awhile, I’m reminded of the old adage: “money can’t buy you happiness”. The other 99.999% of the time, I’m being told by the media that it most certainly can. Companies perform A/B testing until they have optimized click-through rates to the perfect shade of chartreuse. Youtube and Instagram Influencers make a living recording lifestyles so opulent it would make a medieval king jealous.

So…….can money buy us happiness? Was Mr. Krabs right all along?

In this tutorial, we will use SQL commands to merge two datasets with JOIN to find out if money does, in fact, buy happiness. We will then create a simple scatter plot in Jupyter Notebooks using Python Seaborn and SQL Magic.

Finding the Data

You will need to download two datasets from Kaggle; 1) The World Happiness Report and 2) Country Statistics — UN Data. I’d suggest following the two links and downloading them as CSVs to your computer.

Notes:

The UN Data is from 2017, so download the World Happiness Report from 2017 to match.

The UN Data has commas in place of periods to denote decimals. Excel’s Find + Replace fixes the situation beautifully (you can also take the fixed version from my Github repo).

2. Create Table(s) & Upload

Before we can upload the datasets we need a place to put it. We’re going to create two tables, one for the World Happiness data and one for the 2017 UN dataset.

If the columns look little convoluted, and that’s because they are — but we can fix them with some nice code!

Notes:

SQL doesn’t understand special characters or spaces, so replace those with an underscore (“_”) when reading a dataset in.

Adding “NOT NULL” skips any observation that lacks an input in a specified column

The UN Data has 227 countries reported whereas the World Happiness Report has 155; as a result, the number of total observations will match the smaller number after we merge.

If you’re using IBM’s Db2 on Cloud, you can now load in the data (no coding required) by going to “load” and dragging in your CSV files one-by-one. Be sure to click the overwrite function (instead of append).

The UN data should look like this after being loaded:

….and the World Happiness Data:

Great! Now we can get started with combining them.

3. Using JOIN and CREATE VIEW

Next we create a third table that combines the columns we want from both tables. Their “anchor” is going to be the Country column, which both datasets (thankfully) already have. To create the third table, we will use create view function.

Notes:

The * denotes that we want to take all columns from the that dataset.

I use the “as” operator to rename columns so they don’t contain all those pesky underscores (lines 12–15).

After your new view is created, you can call on it just like it was a table!

At this point, I highly encourage you to come up with a few unique queries if you’re following along; there’s no better way to learn than by doing!

5. Findings with SQL

Lets run a few queries to see if countries with high amounts of GDP-per-capita also do well in happiness:

As we can see, of the two top ten lists, only four of the ten were on both; Norway, Denmark, Iceland, and Switzerland. Apparently it pays off to be cold most of the year! That being said, I feel like I only have partial information — it would be nice to see if any trends emerge between these two country metrics.

There’s also a lot of extra columns we aren’t using; for an added challenge, go through some of the more interesting columns and see which seem to correlate the most strongly.

5. Graphing with Python Seaborn

For a better look, we could really benefit from graphing this. Let’s connect our SQL database to Jupyter Notebooks and give it a try (you can download my own notebook here). In order:

5.a) Launch Jupyter Notebooks and create a new notebook.

5.b) Import the libraries you’ll need and connect to the database (code below).

If you’re using IBM you can go to https://cloud.ibm.com/resources → Service → Service Credentials. Copy the line that starts with “uri”, starting with your username and skipping any apostrophes.

5.c) Create a dataframe, import/install seaborn and plot!

End Result:

Happiness Score is on the x-axis, GDP-per-capita is on the y-axis, and each dot signifies a separate country. What kind of trend do you see? Can you figure out ways to make it prettier based on the Seaborn documentation?

7. Conclusions

Do countries with the highest GDP-per-capita also have the highest levels of happiness?

Photo by Josie Josie on Unsplash

TL;DR: Arguably, no. But one could argue there is some correlation.

Long Answer: If you look at any case individually, it’s certainly not one-for-one — only four countries made the top ten of both lists. However, looking at the graph one can see something of a positive relationship (if you’re wondering what that dot near the top of the y-axis is, it’s Luxembourg).

Correlation does not equal causation, but it did bring about some interesting results. For now, we’ll have to settle for some wisdom, instead:

“Money cannot buy happiness; it can, however, rent it.”

-Gordon Livingston