Integrating Python & Tableau
By: Raymond Willey
Tags: Python, Machine Learning, Data Visualization, Tableau, Data Science

Integrating Python & Tableau

Bring your analyses to life with engaging data visualizations.

When performing in-depth analyses on large and unstructured datasets, the power of Python and relevant machine learning libraries cannot be overstated. Matplotlib serves as a great tool to help us visualize results, but it’s stylization options are not always optimal for use in presentations and dashboards. Sure, you can make it work, but visualization software like Tableau is much more suited to helping you tell your story.

To illustrate this, consider a simple example from the King County Housing Dataset. In your exploratory analysis, you are likely to find that location is a strong predictor of price, but you want to know whether to use zip code or GPS coordinates as a predictor. By creating a scatter plot with latitude and longitude coordinates, creating a color map based on price, we can get a sense of where prices are highest.

ax = housing_data.plot.scatter('long', 'lat',

c = 'log_price',

s = housing_data['log_price'],

colormap = 'magma',

figsize=(15, 10),

title='Location-Price Map')

ax.set(xlabel='Longitude', ylabel='Latitude')

plt.show()

Based on this, we get a sense that the relationship between latitude and longitude with price is somewhat parabolic, and from here, you can decide how you want to move forward with the analysis. However, if you wanted to give senior managers a clearer idea of where prices are highest, this visualization wouldn’t be ideal. Using Tableau, we can create something like this:

Can something like this be created with libraries in Python? I would assume so, but software dedicated to data visualization can help you get those results with much more efficiency. What’s more, this software makes it easier to compare different styles with much greater ease. Consider the following infographic, which illustrates the change in heart disease mortality rates across the United States from 1999–2016.

This chart combines the visualizations of Tableau with the animation options in PowerPoint. How do we utilize the visualization prowess of Tableau with the analytic power of Python? I am going to give you three choices and highlight the pros and cons of each. Links to the relevant documentation for implementation can be found at the end of this post.

TabPy

Anyone looking to combine these tools is likely to quickly find the TabPy option. Tableau itself developed TabPy and allows you to run Python scripts within calculated columns in Tableau. After deploying your machine learning module, you can incorporate the call functions in the calculated variable column.

As a use case, consider a machine learning model that predicts house prices based on location and other factors. A home developer may be trying to decide on where to build a new community, and they want to know where they can get the best prices given general characteristics of home design. The business analyst can simply input the details of design and potential locations, and Tableau will provide the relevant map with price predictions instantly.

What’s more, TabPy allows for advanced features, such as authentication management and code publication. However, with such features come additional complications: installation is not always easy, support for legacy Python code makes it run heavy, and script implementation isn’t always clean. But most importantly, TabPy relies on the use of Tableau Server, a paid service. In other words, this will not work with Tableau Public, the free version of Tableau. For these reasons, AltTabPy was created.

AltTabPy

The primary objective of AltTabPy is to simplify and streamline the critical functionality of TabPy. In terms of size, AltTabPy is a mere 5% the size of TabPy, making it incredibly efficient. Installation can be completed with a simple pip command. And incorporating code into Tableau calculated columns is much cleaner.

However, it does not provide the advanced features of TabPy. With this in mind, AltTabPy is an excellent option for a company or individual starting out with limited resources, unburdened by legacy code. The value proposition is effectively the same as TabPy but aimed at a different user. Most importantly, Tableau Server is not required.

CSV Sharing

Though limited in functionality, CSV sharing is the simplest way to send data from Python to Tableau. This option is excellent for the presentation of historical data without the need for predicting new data on the fly. It is also suitable for single analyses that don’t require on-the-fly predictions for new data.

The approach here is as simple as running a machine or deep learning model on relevant data and saving the resulting dataframe to CSV.

df.to_csv('filename.csv')

From here, a Tableau can connect to the relevant file from the Connect menu on the home screen and selecting Text file. From a sustainability perspective, the data scientist should design the code to update the CSV file as new data predictions come available. By refreshing the connection in Tableau, new data can be incorporated into the relevant visualizations.

This strategy can also be adapted to SQL if the data scientist is granted permission to write to SQL servers.

The biggest downside here is that business analysts using Tableau cannot add new data to predict results. Instead, they will have to rely on other teams to generate predictive results before they can be incorporated into visualizations.

Conclusions

TabPy is an end-to-end solution that allows for the true integration of Python and Tableau in team settings. However, it comes with more complexity and higher costs.

AltTabPy provides greater simplicity and doesn’t require an investment in Tableau Server. It is an excellent option for startups using newer versions of Python and who don’t need legacy support.

CSV Sharing is perfect for individuals that don’t need to share data and companies that don’t require fast deployment of machine learning models.

Sources

Official documentation for TabPy and AltTabPy can be found below: