CARTOframes

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

Datasets

In CARTOframes, the Dataset is the generic data class for cartoframes data operations. It is mainly used to work in your Notebook with local data or with data download from your CARTO account and to upload data to your CARTO account. A Dataset instance can be created from a dataframe, geodataframe, a table hosted on a CARTO account, an arbitrary query against a CARTO account, or a local or hosted GeoJSON source.

Table name

Create a new Source passing the name of your table in CARTO:

1
2
3
4
5
6
7
8
9
from cartoframes.auth import set_default_credentials
from cartoframes.data import Dataset

set_default_credentials(
    username='your_user_name',
    api_key='your api key'
)

Dataset('your_table_name')

### SQL Query

1
2
3
4
5
6
7
8
9
from cartoframes.auth import set_default_credentials
from cartoframes.data import Dataset

set_default_credentials(
    username='your_user_name',
    api_key='your api key'
)

Dataset('SELECT * FROM your_table_name LIMIT 10')

GeoJSON

1
2
3
from cartoframes.data import Dataset

Dataset('your_file_name.geojson')

DataFrame

1
2
3
4
5
6
7
from cartoframes.data import Dataset
import pandas as pd

data = {'latitude': [0, 10, 20, 30], 'longitude': [0, 10, 20, 30]}
df = pd.DataFrame.from_dict(data)

Dataset(df)

GeoDataFrame

1
2
3
4
5
6
from cartoframes.data import Dataset
import geopandas

gdf = geopandas.DataFrame(...)

Dataset(gdf)

Download data from CARTO

1
2
3
4
5
6
7
8
9
10
11
from cartoframes.auth import set_default_credentials
from cartoframes.data import Dataset

set_default_credentials(
    username='your_user_name',
    api_key='your api key'
)

ds = Dataset('your_table_name')

df = ds.download()

Upload data to CARTO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from cartoframes.auth import set_default_credentials
from cartoframes.data import Dataset
import pandas as pd

set_default_credentials(
    username='your_user_name',
    api_key='your api key'
)

data = {'latitude': [0, 10, 20, 30], 'longitude': [0, 10, 20, 30]}
df = pd.DataFrame.from_dict(data)
ds = Dataset(df)

ds.upload(table_name='your_new_table_name')