In engineering and applied sciences, the ability to rapidly analyze and visualize data is a critical skill. Whether you are optimizing a circuit, tracking the performance of an autonomous drone, or studying population growth, Python provides the tools to turn raw numbers into actionable insights.
In this introductory module, we will use Python to pull real-world data directly from the New York State government database, clean it, and build an interactive visualization of Suffolk County.
Step 1: Fetching the Data
Instead of downloading files manually, modern engineers use APIs (Application Programming Interfaces) to pull live data directly from the internet. We will use the urllib library to request the data, and pandas to organize it into a readable table called a DataFrame.
Code
import urllib.requestimport jsonimport pandas as pd# 1. Request data from the NY State APIurl ='https://data.ny.gov/resource/34dd-6g2j.json'request = urllib.request.Request(url)response = urllib.request.urlopen(request)# 2. Parse the JSON response into a Pandas DataFrameraw_data = response.read()data_json = json.loads(raw_data)df = pd.json_normalize(data_json)# Display the first 5 rows of our new datasetdf[['county', 'year', 'population']].head()
county
year
population
0
Albany
2024
325175
1
Allegany
2024
47543
2
Bronx
2024
1355521
3
Broome
2024
200084
4
Cattaraugus
2024
75257
Step 2: Cleaning and Isolating the Data
Raw data is rarely ready for immediate analysis. Our dataset contains every county in New York, and the numbers are currently stored as text. We need to convert the text to numeric values and isolate the data specifically for Suffolk County.
Code
# 1. Convert text columns to numeric numbersdf['year'] = pd.to_numeric(df['year'], errors='coerce')df['population'] = pd.to_numeric(df['population'], errors='coerce')# 2. Filter the dataset to only include Suffolk Countysuffolk_data = df[df['county'] =='Suffolk']# Display the cleaned Suffolk County datasuffolk_data[['county', 'year', 'population']].head()
county
year
population
51
Suffolk
2024
1560162.0
113
Suffolk
2023
1535141.0
175
Suffolk
2022
1540104.0
237
Suffolk
2021
1510715.0
299
Suffolk
2020
1465277.0
Step 3: Interactive Visualization
To understand how the population is changing over time, we will use a library called plotly to create an interactive scatter plot. You can hover over the data points below to see the exact figures for each year.
Code
import plotly.express as px# Create an interactive scatter plotfig = px.scatter( suffolk_data, x='year', y='population', title='Population Trends in Suffolk County, NY', labels={'year': 'Year', 'population': 'Total Population'}, color_discrete_sequence=['#1c528b'] # Papillon Engineering Blue)# Enhance the layout for a professional lookfig.update_layout( xaxis=dict(showgrid=True, gridcolor='lightgrey'), yaxis=dict(showgrid=True, gridcolor='lightgrey'), plot_bgcolor='white')fig.show()
Bonus: Engineering Math Functions
Python is also a powerful calculator. When designing cylindrical tanks for sustainable aviation fuel, we frequently need to calculate the area of a circle.
Note: In Python, exponents are written using two asterisks (**), not a caret (^).
Code
import mathdef calculate_circle_area(radius):"""Calculates the area of a circle given its radius.""" area = (radius **2) * math.piprint(f"The area of a circle with a radius of {radius} is: {area:.2f}")# Test the function with a radius of 5calculate_circle_area(5)