Tableau and TabPy

Integrate python to Tableau

Jimmy (xiaoke) Shen
2 min readJan 26, 2021

Introduction

TabPy is something can integrate python to Tableau. I am going to explore this process and this article is used to record this exploration.

Tutorials

Videos

Data science applications with TabPy/R

dataset used in this video

cars.csv

PCA visualization code

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
# read data
df = pd.read_csv("./data/cars.csv")
scale = StandardScaler()
dat = scale.fit_transform(df.iloc[:,1:])
# pca
pca = PCA(n_components = len(df.columns) - 1)
comps = pca.fit_transform(dat)
df_after_pca = pd.DataFrame(comps[:, :3], columns = ["comp 1", "comp 2", "comp 3"])
# vis
df_after_pca.plot(x = "comp 1", y = "comp 2", kind ='scatter', c = df["cyl"], colormap='viridis', legend=False,title="First and second principal components Colored by cyl")
plt.savefig("pca_cars.png", dpi=600)
plt.show()

Visualization output

TabPy is something that can integrate python to Tableau.

Before doing that, we need to add a parameter first, we name this parameter as “Selected PCA Component idx”

Let’s create a Calculated Field named PCA

And put the following code in the PCA field

SCRIPT_REAL("import pandas as pd
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
df = pd.DataFrame({'mpg':_arg1, 'cyl':_arg2, 'disp':_arg3, 'hp':_arg4, 'drat': _arg5, 'wt': _arg6, 'qsec': _arg7})
scale = StandardScaler()
dat = scale.fit_transform(df.iloc[:,1:])
pca = PCA(n_components = len(df.columns) - 1)
comps = pca.fit_transform(dat)
return list(comps[:, _arg8[0]])",
SUM([Mpg]),
SUM([Cyl]),
SUM([Disp]),
SUM([Hp]),
SUM([Drat]),
SUM([Wt]),
SUM([Qsec]),
[Selected PCA Component idx])

Quick explanation about the above code

SCRIPT_REAL: means we are going to return something with data type as real numberreturn list(comps[:, _arg8[0]])",   ----> this is the return, _arg8[0] is the first element of the list [Selected PCA Component idx]SUM([Mpg]),  ---> this is _arg1
SUM([Cyl]), ---> this is _arg2
SUM([Disp]), ...
SUM([Hp]), ...
SUM([Drat]), ...
SUM([Wt]), ...
SUM([Qsec]), ...
[Selected PCA Component idx] ---> this is _arg8)

Some nice examples can be found here.

--

--