Raw Data to Interactive Maps in the Blink of an Eye with GPT4 Prompting
Interactive UN population projection mapping with Python Plotly

ChatGPT is a terrific tool for generating data cleaning and visualization code in Python.
In combination with the right libraries — Pandas and Plotly — you can create fantastically pleasing and interactive maps.
Let’s test this all out by loading up some awesome and interesting UN population projection data for GPT-4 to work its magic with.
To better understand how to prompt engineer GPT-4 to create Python code, we can break it down to:
- cleaning a dataset
- creating a choropleth map
- creating an animated choropleth map that illustrates data over time
As always, the best way to learn is to give it a go! Here’s how!
Step 1: Data Cleaning
For this exercise, we will be using a newly updated dataset from the UN Department of Economic and Social Affairs website (HERE).
This dataset models population growth projections from 2022 to 2100.
After a cursory view, I can see that some data cleaning is in order. The actual data headers start on Row 17. So we can remove the first 16 Rows of data (or just start our data retrieval at this row).
Now, if we want to do a choropleth map to show each country over time (by heat map), then we usually only want the rows that actually have a value for the 3-letter ISO field.
I can see by the observation that if I only use the rows that have a value in this column, I will essentially cover every country on the planet. Cool. That’s what I want.
So let’s engineer a prompt to ChatGPT to create a new CSV file based on this criteria (click the attachment icon and upload your file first):

Response from ChatGPT:
import pandas as pd
# Load the CSV file into a DataFrame
file_path = '/mnt/data/pp_median.csv'
df = pd.read_csv(file_path)
# Filter the DataFrame to include only rows where the "ISO3 Alpha-code" field has a value
filtered_df = df[df['ISO3 Alpha-code'].notna()]
filtered_df.head() # Display the first few rows of the filtered DataFrameThis code first uses the pandas library to load a CSV file into a pandas data frame. The pd.read_csv function is used to read the CSV file.
The data frame (df) is then filtered to only include rows where the ‘ISO3 Alpha-code’ field has a value. This is done using the df['ISO3 Alpha-code'].notna() condition, which returns a boolean Series where True represents rows where 'ISO3 Alpha-code' is not null.
OK, so it looks like GPT-4 has an understanding of our data set.
Now, we can go on to map creation!
Step 2: Simple Choropleth Map for a Single-Year
Now that we have our data frame ready to go, we’re ready to map!
To start with, for simplicity's sake, let’s just create a choropleth map for a single year. Going into more detail within our data set, we will be using the following fields;
- ISO3 Alpha-code: The ISO 3166-1 alpha-3 code for the country. This is a three-letter code that represents each country uniquely. This field is used for creating the choropleth map.
- Year: The year to which the data pertains.
- Age group columns (
0-14,0-19,0-24,15-24,15-49,15-59,15-64,20-64,20-69,25-64,25-69,60,65,70,80): These columns represent different age groups and contain the percentage of the population that falls into each age group for the given year and location.
With this knowledge in hand, let’s go ahead and prompt engineer ChatGPT to generate the code for a choropleth map that shows the percentage of the population 65 years and older for the projected year 2024 for each country.
Prompt to ChatGPT: Using the new CSV file, please write the Python code to create a Plotly choropleth map that displays this data for the Year 2024 and for the Age group “65”.
Response from ChatGPT:
import pandas as pd
import plotly.express as px
# Load the CSV file into a DataFrame
file_path = '/mnt/data/pp_median.csv'
df = pd.read_csv(file_path)
# Filter the DataFrame to include only rows where the "ISO3 Alpha-code" field has a value
filtered_df = df[df['ISO3 Alpha-code'].notna()]
# Filter the DataFrame for the year 2024 and age group "65"
year_2024_df = filtered_df[filtered_df['Year'] == 2024]
data_65_age_group = year_2024_df[['ISO3 Alpha-code', '65']]
# Create a Plotly choropleth map
fig = px.choropleth(
data_65_age_group,
locations="ISO3 Alpha-code",
color="65",
hover_name="ISO3 Alpha-code",
color_continuous_scale=px.colors.sequential.Plasma,
title="Choropleth Map for Age Group 65 in the Year 2024"
)
fig.show()NOTE: with the file_path variable, make sure the path to your CSV is correct for the location of your file CSV file.
This code first uses the pandas library to load the filtered CSV file into a DataFrame. The DataFrame is then filtered to only include rows for the year 2024 and for all people who are projected to be 65 years of age or older.
The Plotly Express function px.choropleth is used to create a choropleth map.
Now you can Copy/Paste into your favorite Python editor (I use Pycharm), click Save, and then click Run.
In your default web browser, you should see something similar to:

Achh, this is not quite right though — I think I know what the issue is — we need to make sure that the value for each country is numeric, not a text value.
And we need to make sure we are using a “continuous” color scale, not a “sequential” color scale.
So, another prompt to GPT-4 just to clear this up:
Prompt to GPT-4: Please make sure the values for the field ‘65’ are numeric. And please make it a “continuous” scale for the map.
For brevity, just the revised lines of code from GPT-4:
# Ensure the '65' column is numeric for a continuous scale
data_65_age_group['65'] = pd.to_numeric(data_65_age_group['65'], errors='coerce')And within our choropleth map properties (revise 1 line):
color_continuous_scale="Rainbow", # Using a rainbow color scaleAnd our new map:

The resulting map shows the projected percentage of the population 65 years or older for the year 2024. We can see that for this example, Japan currently has a very old population relative to the rest of the world.
A more interesting analysis would be to see how each country ages over a period of time.
The CSV file we are using has projections from 2022 up to the year 2100.
So, let’s see if we can create an animated map that starts in 2022 and shows us the projected values for each country up until the year 2100.
Step 3: Animated Choropleth Map for All Years
Now, we can take it up a notch and ask for some more functionality — some animation across time, which tells a much deeper story about the projected trends, particularly for aging populations — which is a concern.
Prompt to ChatGPT: Using the same CSV file, use the full date range (2022–2100), and make sure the ‘Year” does NOT display a decimal value. Then, set the color range to a static value — from 0 to 50 and the color_continuous_scale to ‘rainbow’.
Response from ChatGPT:
import plotly.express as px
import pandas as pd
# Load the DataFrame
file_path = 'pp_median.csv'
df = pd.read_csv(file_path)
# Filter to include only rows where "ISO3 Alpha-code" is not null
filtered_df = df[df['ISO3 Alpha-code'].notna()]
# Convert the 'Year' column to integer to remove decimal values
filtered_df['Year'] = filtered_df['Year'].astype(int)
# Filter the DataFrame for the age group "65" for all years in the range 2022-2100
age_65_data = filtered_df[(filtered_df['Year'] >= 2022) & (filtered_df['Year'] <= 2100)][['ISO3 Alpha-code', 'Year', '65']]
# Ensure the '65' column is numeric for a continuous scale
age_65_data['65'] = pd.to_numeric(age_65_data['65'], errors='coerce')
# Create a Plotly choropleth map with a static color range from 0 to 50 and a rainbow color scale
fig = px.choropleth(
age_65_data,
locations="ISO3 Alpha-code",
color="65",
hover_name="ISO3 Alpha-code",
animation_frame="Year",
color_continuous_scale="Rainbow", # Using a rainbow color scale
range_color=[0, 50], # Static color range from 0 to 50
title="Interactive Map for Age Group 65 from 2022 to 2100"
)
fig.show()The above code was generated for me by GPT-4 based on the prompt I provided. As you may be aware, GPT-4 is not always consistent in its responses.
However, I have noticed that over time, its ability to produce accurate Python Plotly code is most certainly improving.
You can Copy/Paste into your favourite Python editor (I use Pycharm), click Save, and then click Run.
NOTE: Make sure you set the correct path to your CSV file (change the file_path variable)! Mine is in the same directory as my .py file.
The px.choropleth function is used to create an animated choropleth map. The animation_frame parameter is set to 'Year', which creates an animation over the years. The color_continuous_scale parameter is set to 'Rainbow', which defines the color scale of the map.
The range_color parameter is set to (0, 50), which sets the color range and keeps it consistent from year to year (gives a consistent color for each percentage as we go from year to year).
Here is a video animation of our animated map “in action”:

Yes, it works! And it is super cool! We can see the aging global population over time with one click of a button.
As we move forward in time, there are certain areas of the world (Italy, Japan, and South Korea being vivid examples) that are projecting towards a significantly older population.
This potentially puts heavier burdens on the workforce, on medical systems, and on national retirement and pension policies.
Nice work putting this all together!
Conclusion
GPT-4 is a powerful tool for data cleaning and visualization when used with the right prompts.
Particularly now that you can do all of this from the main ChatGPT window, including uploading your CSV file for on-the-fly analysis and code generation.
Combined with Plotly, you can quickly and seamlessly create animated geospatial data visualizations that tell stories over time.
What is still missing from the GPT-4 repertoire is the ability to create interactive Python Plotly visuals from the main GPT-4 window.
Currently (as of Dec/2023), GPT-4 can only visualize non-interactive data visualizations created with the matplotlib library.
I am hoping that in the next iteration, this Plotly interactivity will magically appear!
Thanks for reading!
If this type of story is right up your alley, and you want to support me as a writer, subscribe to my Substack. It’s free and has more stories like this one.
On Substack, I publish articles that you won’t find on the other platforms where I create content.






