Power up your Spatial Data Science workflow with CARTOframes.
By: Ransaka Ravihara
Tags: GIS, Data Science, Python, Spatialdatascience, Data Analysis

In this tutorial, we will construe the ways we can use CARTOframes with python to build dashboards and share your insights with your audience.

A little bit about the CARTO platform,

CARTO is one of the leaders in the Location Intelligence domain. It's beneficial for solve location intelligence problems of many industries including,

1. Telecommunication

2. Transport

3. Logistics, and many more.

There are few main components of the CARTO platform.

Dashboard

Builder

Platform APIs and libraries

Data Observatory

CARTOframes

In this article, we are going to explore the basic capabilities of CARTOframes by building an interactive dashboard. For that, we will use gun-violence-data from Kaggle.

Account creation, Install and basic authentication setup

Account Creation

NOTE: This step is optional if you will use CARTOframes to visualize your local data, but If you want to share it with the audience or need to use some advanced features, you definitely need a CART account to process further.

If you are a student or educator, you can use your academic email to get a student account using Github Education Pack.

After account creation, you can access your API key from your CARTO dashboard. Now you are ready to set up authentication for your local application. All you need is to get your API key and username and save it as a JSON file in the following format. Name it as creds.jon and save it in your current working directory.

creds.json

2. Install

CARTOframes requires Shapely and Fiona libraries already in your system. Hence, use the following commands to install it correctly,

pip install shapely

conda install -c conda-forge fiona

pip install cartoframes

3. Setup Authentication

Now you have installed CARTOframes in your machine and saved your credentials. Then, you can set the Authentication using the following lines of code.

from cartoframes.auth import set_default_credentials

set_default_credentials(‘creds.json’)

Alright! Now we can move to the next stage.

Import data and pre-processing

You can see the description of the data using the Kaggle site. It contains 239,677 incidents for the year 2013 to 2018.

Sample of the dataset.

sample

Afterward, we select incidents that occurred in the year 2018 and few columns only.

Now we have the following data frame,

Now we need clear participant gender columns according to our use case. We will get participants' male count and female count separately.

cleaned data

Visualization

To visualize and spatial data frame with CARTOframes the source data frame geometry’s CRS must be WGS 84 (EPSG:4326).

The easiest way is converting our pandas.DataFrame to geopandas.GeoDataFrame.

Now we can visualize our data.

Another useful component of CARTOframes is widgets, styles, and popups. We can select an appropriate widget based on our use case. In this visualization, we will use,

Click and hover popups -Display a popup triggered by a hover event Histogram widget -Create a widget to represent categorical, numeric, and date values in a histogram Category widget -Create a widget to represent categorical values Formula widget -Display the result of a count, avg, max, min, or sum operation in a numeric Animation style -To be able to play, pause and change an animated visualization through animation controls

let's see how to implement it using python,

Before that, we need to create a new column called weekday

incidents_2018_gdf['weekday'] = (incidents_2018_gdf['date']

.dt.day_name())

Next, we will create another layer containing an animation style for the date column,

Now our main dashboard ready to use, additionally you can publish it using the following way. You have to set default credentials before doing this.

map_viz.publish('viz_name',password='letmein',if_exists='replace')

Also don't forget to visit the documentation, in case you got stucked😉

Thanks!