{ "cells": [ { "cell_type": "markdown", "id": "368e0ff0-d7f6-4ba2-8891-de2a39db4b8e", "metadata": {}, "source": [ "# ChatGPT Python Code Writer for Analyzing Datasets\n", "With examples using Starbucks location data! ☕️ " ] }, { "cell_type": "markdown", "id": "b4a0410b-805e-4d1a-8758-a9e57691c4cc", "metadata": {}, "source": [ "## Step 1: Import (a lot of) libraries" ] }, { "cell_type": "code", "execution_count": 79, "id": "95e21d55-52e5-4f34-8988-f30bdbb51847", "metadata": {}, "outputs": [], "source": [ "# Since we cannot be certain of what kind of code ChatGPT will come up with, \n", "# we start by installing a bunch of libraries it may use in its response. \n", "# Lots of these are for making pretty maps. \n", "\n", "# Basics\n", "import pandas as pd\n", "import openai\n", "import os\n", "\n", "# Mapping\n", "from geopy import distance\n", "from geopy.geocoders import Nominatim\n", "import geopandas as gpd\n", "from shapely.geometry import Point, Polygon\n", "from geopy.distance import geodesic\n", "import folium\n", "\n", "# Charts\n", "import seaborn as sns\n", "import matplotlib.pyplot as plt\n", "\n", "# Parsing text, displaying results in markdown\n", "from IPython.display import display, Markdown, Latex\n", "import re" ] }, { "cell_type": "markdown", "id": "774cbaf9-02ff-49fe-9d49-2dfa51799ee6", "metadata": {}, "source": [ "## Step 2: Set up your [OpenAI key](https://platform.openai.com/)" ] }, { "cell_type": "code", "execution_count": 80, "id": "c3f6aae9-33a5-4559-a49a-ae9812a17664", "metadata": {}, "outputs": [], "source": [ "## OPENAI KEY ##\n", "openai.api_key = os.environ.get('OPENAI_KEY')" ] }, { "cell_type": "markdown", "id": "fd087429-5278-45ca-86e5-3f11f8b48fd3", "metadata": {}, "source": [ "## Step 3. Load your data and parse inputs for the OpenAI query\n", "This sample dataset is a directory of Starbucks locations, scraped from the Starbucks store locator webpage by Github user [chrismeller](https://github.com/chrismeller/). Geospatial coordinates have been truncated, just in case..." ] }, { "cell_type": "code", "execution_count": 81, "id": "6e27b49f-3637-4776-b2a3-9f019966d618", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | Unnamed: 0.1 | \n", "Unnamed: 0 | \n", "Brand | \n", "Store Number | \n", "Store Name | \n", "Ownership Type | \n", "Street Address | \n", "City | \n", "State/Province | \n", "Country | \n", "Postcode | \n", "Phone Number | \n", "Timezone | \n", "Longitude | \n", "Latitude | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "0 | \n", "0 | \n", "Starbucks | \n", "47370-257954 | \n", "Meritxell, 96 | \n", "Licensed | \n", "Av. Meritxell, 96 | \n", "Andorra la Vella | \n", "7 | \n", "AD | \n", "AD500 | \n", "376818720 | \n", "GMT+1:00 Europe/Andorra | \n", "1.53 | \n", "42.51 | \n", "
1 | \n", "1 | \n", "11 | \n", "Starbucks | \n", "1579-122101 | \n", "HCT Abu Dhabi Women's College Block | \n", "Licensed | \n", "Najda Street, Higher Colleges of Technology | \n", "Abu Dhabi | \n", "AZ | \n", "AE | \n", "3167 | \n", "26426280 | \n", "GMT+04:00 Asia/Dubai | \n", "54.37 | \n", "24.49 | \n", "
2 | \n", "2 | \n", "12 | \n", "Starbucks | \n", "32595-122105 | \n", "Standard Chartered Building | \n", "Licensed | \n", "Khalidiya St., Beside Union Cooperative Society | \n", "Abu Dhabi | \n", "AZ | \n", "AE | \n", "3167 | \n", "26359275 | \n", "GMT+04:00 Asia/Muscat | \n", "55.69 | \n", "24.19 | \n", "
3 | \n", "3 | \n", "20 | \n", "Starbucks | \n", "32767-131566 | \n", "Shangri-La Souq | \n", "Licensed | \n", "Shangri-La Souk, Um Al Nar | \n", "Abu Dhabi | \n", "AZ | \n", "AE | \n", "3167 | \n", "25581641 | \n", "GMT+04:00 Asia/Dubai | \n", "54.51 | \n", "24.42 | \n", "
4 | \n", "4 | \n", "45 | \n", "Starbucks | \n", "32640-131563 | \n", "Tawam Hospital | \n", "Licensed | \n", "Al Ain Abu Dhabi Rd, Khalifa Bin Zayed, Al Mak... | \n", "Al Ain | \n", "AZ | \n", "AE | \n", "3167 | \n", "37677581 | \n", "GMT+04:00 Asia/Muscat | \n", "55.65 | \n", "24.19 | \n", "