Data Viz using Python and Tableau: from API call to Hosted Dashboard | by Richard Peterson | Towards Data Science

Building an infographic often starts while you are first exploring the data. Matplotlib, Seaborn, and ggplot2 are the classic programming charts, while Plotly has also exploded in popularity. But for me, nothing matches the clean professional aesthetic of Tableau. In this article, we will leave behind the inline Jupyter Notebook charts and learn how to create hosted interactive charts using Tableau Public.
This article is comprised of three parts:
Research — what is your topic and how will you extract the data? In this article, we will check out the Bureau of Labor Statistics (BLS) API. We are able to retrieve data using an HTTP POST request with parameters including the query IDs for each BLS query. The Data we will request is Unemployment by State.Data Cleaning — Using python, we will call the BLS API and clean the data using Pandas into a tidy vertical format. This can be done with a Jupyter Notebook or Python Script. Data will be saved to a CSV file and SQLite database.Tableau — Create a choropleth (geographic) map and a tree map, and publish the charts to Tableau Public. We will be able to share the chart with an embedded link.
Research — what is your topic and how will you extract the data? In this article, we will check out the Bureau of Labor Statistics (BLS) API. We are able to retrieve data using an HTTP POST request with parameters including the query IDs for each BLS query. The Data we will request is Unemployment by State.
Data Cleaning — Using python, we will call the BLS API and clean the data using Pandas into a tidy vertical format. This can be done with a Jupyter Notebook or Python Script. Data will be saved to a CSV file and SQLite database.
Tableau — Create a choropleth (geographic) map and a tree map, and publish the charts to Tableau Public. We will be able to share the chart with an embedded link.
You can follow along with the querying and cleaning data section from my GitHub repo! This is a companion article to an analysis on unemployment from the COVID-19 pandemic.
github.com
The Bureau of Labor Statistics API is a REST API that serves data based on unique query IDs. BLS query IDs are based on a number of factors, such as state and industry. To find the query ID, you will need to browse to BLS Databases, Tables & Calculators and locate the category related to your data. Unemployment by State is found under Local Area Unemployment Statistics — One-Screen Search. You will notice that each query has a unique ID. If you can determine the pattern, you can programmatically generate these IDs rather than copying and pasting.
In the GitHub repo, there is a module named blspandas.py which has wrapper functions for cleaning and performing the HTTP POST request that retrieves the data (Credit: BD Economics).The below script calls these functions to create a list of query IDs and performs the HTTP request, and saves the results as a pandas dataframe.
A key part of the blspandas.py module is the clean_bls_data(combined_queries) function. Before we ran df = blspandas.clean_bls_data(combined_queries) our data looked like this:
We need this data in a tidy vertical format like State | Date | Metric. Pandas melt is perfect for this transformation.
After running df = blspandas.clean_bls_data(combined_queries), the data looks like this:
For this, let’s think about what we ultimately need. Our goal here is to create a choropleth map and a tree map showing the change in unemployment from January 2020 to June 2020. This will describe the effects of the COVID-19 pandemic on the labor market, and show which states have not bounced back.
What we need to do is find the difference in unemployment between two dates for every state. We will do this by locating all the Date rows that contain our two dates, and subtract January unemployment from June Unemployment.
The resulting chg_emp dataframe looks like this:
This is all we need to create our Tableau Visuals! Simply write the chg_emp dataframe to a CSV file by running:
chg_emp.to_csv('Change in unemployment by state.csv', index=False)
At this point, either sign up for Tableau Public, or purchase the software. As a student, I was able to sign up for free and received the full desktop version.
Load the data by importing your CSV file into your workbook. Click on new worksheet and configure it like the below:
For the Tree Map, we will display the same data, but in a way that allows for greater detail, including the state’s name and change in unemployment. Under Marks, you will need:
Label for StateColor for Percent UnemployedLabel for Percent Unemployed
Label for State
Color for Percent Unemployed
Label for Percent Unemployed
You can create a dashboard by clicking on the ‘Dashboard 1’ and dragging over your two worksheets. Here I have arranged the Choropleth on top of the Tree Map.
Finally, we will push our Tableau workbook to Tableau Public.
To do this, we need to Extract our CSV data source. This will allow for the workbook to be uploaded as a single, bundled file to Tableau Public.
Once the extract is completed, we can push our workbook to Tableau Public.
After the upload to Tableau, it will open in your browser window. At this point, all you need to share your dashboard is to click the share button in the bottom right and send the link to your friends! Check out my version of the hosted dashboard here!
Embedded Dashboard
You have a couple of options:
Embed script — this can be embedded into html to serve a live version of your dashboard from Tableau Public. This article demonstrates how live dashboards can be embedded into a WordPress blog post.Unfortunately, you cannot embed the live dashboard in a Medium article.Another good option to check out is embedding the dashboard on a simple python web server.
Embed script — this can be embedded into html to serve a live version of your dashboard from Tableau Public. This article demonstrates how live dashboards can be embedded into a WordPress blog post.
Unfortunately, you cannot embed the live dashboard in a Medium article.
Another good option to check out is embedding the dashboard on a simple python web server.
Thanks for reading, and I hope you find the python+Tableau stack as compelling as I do!