Skip to main content

CARTO Python package for data scientists

Project description

https://travis-ci.org/CartoDB/cartoframes.svg?branch=master https://coveralls.io/repos/github/CartoDB/cartoframes/badge.svg?branch=master https://mybinder.org/badge.svg

A Python package for integrating CARTO maps, analysis, and data services into data science workflows.

Python data analysis workflows often rely on the de facto standards pandas and Jupyter notebooks. Integrating CARTO into this workflow saves data scientists time and energy by not having to export datasets as files or retain multiple copies of the data. Instead, CARTOframes give the ability to communicate reproducible analysis while providing the ability to gain from CARTO’s services like hosted, dynamic or static maps and Data Observatory augmentation.

Features

  • Write pandas DataFrames to CARTO tables

  • Read CARTO tables and queries into pandas DataFrames

  • Create customizable, interactive CARTO maps in a Jupyter notebook

  • Interact with CARTO’s Data Observatory

  • Use CARTO’s spatially-enabled database for analysis

More info

Install Instructions

To install cartoframes (currently in beta) on your machine, do the following to install the latest pre-release version:

$ pip install cartoframes

It is recommended to use cartoframes in Jupyter Notebooks (pip install jupyter). See the example usage section below or notebooks in the examples directory for using cartoframes in that environment.

Virtual Environment

To setup cartoframes and Jupyter in a virtual environment:

$ virtualenv venv
$ source venv/bin/activate
(venv) $ pip install cartoframes
(venv) $ pip install jupyter
(venv) $ jupyter notebook

Then create a new notebook and try the example code snippets below with tables that are in your CARTO account.

Example usage

Data workflow

Get table from CARTO, make changes in pandas, sync updates with CARTO:

import cartoframes
# `base_url`s are of the form `http://{username}.carto.com/` for most users
cc = cartoframes.CartoContext(base_url='https://eschbacher.carto.com/',
                              api_key=APIKEY)

# read a table from your CARTO account to a DataFrame
df = cc.read('brooklyn_poverty_census_tracts')

# do fancy pandas operations (add/drop columns, change values, etc.)
df['poverty_per_pop'] = df['poverty_count'] / df['total_population']

# updates CARTO table with all changes from this session
cc.write(df, 'brooklyn_poverty_census_tracts', overwrite=True)

Write an existing pandas DataFrame to CARTO.

import pandas as pd
import cartoframes
df = pd.read_csv('acadia_biodiversity.csv')
cc = cartoframes.CartoContext(base_url=BASEURL,
                              api_key=APIKEY)
cc.write(df, 'acadia_biodiversity')

Map workflow

The following will embed a CARTO map in a Jupyter notebook, allowing for custom styling of the maps driven by TurboCARTO and CARTOColors. See the CARTOColors wiki for a full list of available color schemes.

from cartoframes import Layer, BaseMap, styling
cc = cartoframes.CartoContext(base_url=BASEURL,
                              api_key=APIKEY)
cc.map(layers=[BaseMap('light'),
               Layer('acadia_biodiversity',
                     color={'column': 'simpson_index',
                            'scheme': styling.tealRose(5)}),
               Layer('peregrine_falcon_nest_sites',
                     size='num_eggs',
                     color={'column': 'bird_id',
                            'scheme': styling.vivid(10)})],
       interactive=True)

Augment from Data Observatory

Interact with CARTO’s Data Observatory:

import cartoframes
cc = cartoframes.CartoContext(BASEURL, APIKEY)

# total pop, high school diploma (normalized), median income, poverty status (normalized)
# See Data Observatory catalog for codes: https://cartodb.github.io/bigmetadata/index.html
data_obs_measures = [{'numer_id': 'us.census.acs.B01003001'},
                     {'numer_id': 'us.census.acs.B15003017',
                      'normalization': 'predenominated'},
                     {'numer_id': 'us.census.acs.B19013001'},
                     {'numer_id': 'us.census.acs.B17001002',
                      'normalization': 'predenominated'},]
df = cc.data('transactions', data_obs_measures)

CARTO Credential Management

Save and update your CARTO credentials for later use.

from cartoframes import Credentials, CartoContext
creds = Credentials(username='eschbacher', key='abcdefg')
creds.save()  # save credentials for later use (not dependent on Python session)

Once you save your credentials, you can get started in future sessions more quickly:

from cartoframes import CartoContext
cc = CartoContext()  # automatically loads credentials if previously saved

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cartoframes-0.4.2b8.tar.gz (56.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cartoframes-0.4.2b8-py2.py3-none-any.whl (45.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file cartoframes-0.4.2b8.tar.gz.

File metadata

  • Download URL: cartoframes-0.4.2b8.tar.gz
  • Upload date:
  • Size: 56.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for cartoframes-0.4.2b8.tar.gz
Algorithm Hash digest
SHA256 475bbef27fea88d915d761db0c94cca3b141b6daa308be3f92c23a1f4128587e
MD5 9eebb40bbe86ecf3c4d58cd0fd103085
BLAKE2b-256 45491f33d8a268ab4c667daf9932d5f64c7fb4cfc62f9f30c5df971ef395f4f6

See more details on using hashes here.

File details

Details for the file cartoframes-0.4.2b8-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for cartoframes-0.4.2b8-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e287317cd36bacfc280d57b149942d1c6382ae63a7f957da63ec108f8fe422f3
MD5 df66255804cf02ccfafc2e28a9400fb2
BLAKE2b-256 4fe291db11e0028eda3ba0a2d5af193d365a54577edf8aa5450bce361a4dda14

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page