Pydeck, Python data visualization. Column Layer on Carto maps


I will be exploring deck.gl geo data visualisation capabilities with Python and pandas library. Deck.gl is a WebGL-powered visualization framework for large-scale datasets, and pydeck is a high-scale spatial rendering for Python (powered by deck.gl).
Want to read it on Medium? Click here
LET'S GET STARTED
For this project, I am using Jupyter Notebook — the original web application for creating and sharing computational documents. It offers a simple, streamlined, document-centric experience.
The key Python library for this project is pydeck. By default, pydeck 0.6 provides basemap tiles through Carto. You can optionally use Mapbox API key, by registering for Mapbox. But, the Carto map is enough for this project.
Let's import pydeck via pip:
1pip install pydeck
I prepared the data set of random store sales in 2020 across Estonia (a totally randomized dataset).
1. We start by importing essential Python libraries for our operations: pandas, pydeck, os and optionally matplotlib.pyplot (I may develop this post in future)
1import pandas as pd2 import pydeck as pdk3 from pydeck.types import String4 import matplotlib.pyplot as plt5 import os
2. Let’s list the files in the current directory and check if our .xlsx data source is stored here. We then import .xlsx data to Pandas Dataframe and set the index to store location name (in our case, numeric id).
1os.listdir() ## checking the current file composition of the folder2 df = pd.read_excel('stores.xlsx') ## reading .xlsx file to Pandas DataFrame3 df.set_index('Location Name') ## setting indexing to store's name

3. We check the current columns of pandas DataFrame
1df.columns2 # output3 # Index(['Location Name', 'Address line 1', 'Address line 2', 'Address line 3',4 # 'City Name', 'Latitude', 'Longitude', 'Lat', 'Long', 'Postal Code',5 # 'Results'],6 # dtype='object')
4. and create new dataFrame df2 extracting the info we may need.
1df2 = df[['Location Name', 'City Name', 'Latitude', 'Longitude', 'Lat', 'Long', 'Postal Code', 'Results']]2 df2
5. New df2 DataFrame would look like this:

6. We ensure that longitude and latitude are of the ‘float’ type readable by pydeck class.
1df2["Long"] = df2["Long"].astype(float)2 df2["Lat"] = df2["Lat"].astype(float)3 df2
7. We create the instance of Deck class with the selection of ColumnLayer and subsequent parameters.
1layer = pdk.Layer(2 'ColumnLayer',3 df2,4 get_position=['Long', 'Lat'],5 get_elevation=["Results"],6 auto_highlight=True,7 elevation_scale=5,8 pickable=True,9 extruded=True,10 get_radius=100,11 get_fill_color=["Results * 10", "Results", "Results * 40", 220],12 coverage=113 )
8. We set the initial viewport location on the geo area where we plot our data.
1# Set the viewport location2 view_state = pdk.ViewState(3 longitude=25.336900,4 latitude=54.734235,5 zoom=2,6 min_zoom=5,7 max_zoom=15,8 pitch=40.5,9 bearing=-27.36)
9, Then we store all respective parameters in the variable, and pydeck allows us to create ultra light HTML (in this case, based on CARTO MAP) with the ColumnLayer plot of our store results.
1r = pdk.Deck(layers=[layer], initial_view_state=view_state)2 r.to_html('stores.html')

Great! I think this was a nice and quick walk through how to use pydeck, Carto maps and Pandas library to prepare the dataset to plot on the map. I hope you enjoyed it.
Happy coding :)
Piotr

