Writing to CARTO

Writing the shapefile to the CARTO account requires only a CartoContext object, a file path, and the usual URL and API key combination. With GeoPandas now installed, the MLB Stadiums shapefile can be loaded into a GeoPandas DataFrame, and then written to the CARTO account using the CartoContext write method:

import geopandas as gdpimport cartoframesAPIKEY = "{API KEY}"cc = cartoframes.CartoContext(base_url='https://{username}.carto.com/',                              api_key=APIKEY)shp = r"C:\Data\Stadiums_MLB\Stadiums_MLB.shp"data = gdp.read_file(shp)cc.write(data,"stadiums_mlb")

Log in to the CARTO account to confirm that the dataset has been added.

Get Mastering Geospatial Analysis with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.