DATA VISUALIZATION
Task
- Import required libraries
- Load dataset
- Plot the data
1. Imported the required libraries
In [10]:
# ---------------------------------------------------------
# 1. Import the required libraries
# ---------------------------------------------------------
import pandas as pd
import matplotlib.pyplot as plt
!pip install folium
import folium
from folium.plugins import HeatMap
Requirement already satisfied: folium in /opt/conda/lib/python3.13/site-packages (0.20.0) Requirement already satisfied: branca>=0.6.0 in /opt/conda/lib/python3.13/site-packages (from folium) (0.8.2) Requirement already satisfied: jinja2>=2.9 in /opt/conda/lib/python3.13/site-packages (from folium) (3.1.6) Requirement already satisfied: numpy in /opt/conda/lib/python3.13/site-packages (from folium) (2.3.3) Requirement already satisfied: requests in /opt/conda/lib/python3.13/site-packages (from folium) (2.32.5) Requirement already satisfied: xyzservices in /opt/conda/lib/python3.13/site-packages (from folium) (2025.4.0) Requirement already satisfied: MarkupSafe>=2.0 in /opt/conda/lib/python3.13/site-packages (from jinja2>=2.9->folium) (3.0.3) Requirement already satisfied: charset_normalizer<4,>=2 in /opt/conda/lib/python3.13/site-packages (from requests->folium) (3.4.4) Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.13/site-packages (from requests->folium) (3.11) Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.13/site-packages (from requests->folium) (2.5.0) Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.13/site-packages (from requests->folium) (2025.10.5)
2. Loaded dataset
In [15]:
# ---------------------------------------------------------
# 2. Load your dataset
# ---------------------------------------------------------
df2 = pd.read_csv("datasets/Motor_Vehicle_Collisions_-_Crashes.csv", low_memory=False)
# Show first 50 rows
df2.head(50)
Out[15]:
| CRASH DATE | CRASH TIME | BOROUGH | ZIP CODE | LATITUDE | LONGITUDE | LOCATION | ON STREET NAME | CROSS STREET NAME | OFF STREET NAME | ... | CONTRIBUTING FACTOR VEHICLE 2 | CONTRIBUTING FACTOR VEHICLE 3 | CONTRIBUTING FACTOR VEHICLE 4 | CONTRIBUTING FACTOR VEHICLE 5 | COLLISION_ID | VEHICLE TYPE CODE 1 | VEHICLE TYPE CODE 2 | VEHICLE TYPE CODE 3 | VEHICLE TYPE CODE 4 | VEHICLE TYPE CODE 5 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 09/11/2021 | 2:39 | NaN | NaN | NaN | NaN | NaN | WHITESTONE EXPRESSWAY | 20 AVENUE | NaN | ... | Unspecified | NaN | NaN | NaN | 4455765 | Sedan | Sedan | NaN | NaN | NaN |
| 1 | 03/26/2022 | 11:45 | NaN | NaN | NaN | NaN | NaN | QUEENSBORO BRIDGE UPPER | NaN | NaN | ... | NaN | NaN | NaN | NaN | 4513547 | Sedan | NaN | NaN | NaN | NaN |
| 2 | 11/01/2023 | 1:29 | BROOKLYN | 11230 | 40.621790 | -73.970024 | (40.62179, -73.970024) | OCEAN PARKWAY | AVENUE K | NaN | ... | Unspecified | Unspecified | NaN | NaN | 4675373 | Moped | Sedan | Sedan | NaN | NaN |
| 3 | 06/29/2022 | 6:55 | NaN | NaN | NaN | NaN | NaN | THROGS NECK BRIDGE | NaN | NaN | ... | Unspecified | NaN | NaN | NaN | 4541903 | Sedan | Pick-up Truck | NaN | NaN | NaN |
| 4 | 09/21/2022 | 13:21 | NaN | NaN | NaN | NaN | NaN | BROOKLYN BRIDGE | NaN | NaN | ... | Unspecified | NaN | NaN | NaN | 4566131 | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN | NaN |
| 5 | 04/26/2023 | 13:30 | NaN | NaN | NaN | NaN | NaN | WEST 54 STREET | NaN | NaN | ... | Unspecified | NaN | NaN | NaN | 4623759 | Sedan | Box Truck | NaN | NaN | NaN |
| 6 | 11/01/2023 | 7:12 | NaN | NaN | NaN | NaN | NaN | HUTCHINSON RIVER PARKWAY | NaN | NaN | ... | Driver Inattention/Distraction | NaN | NaN | NaN | 4675709 | Sedan | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN |
| 7 | 11/01/2023 | 8:01 | NaN | NaN | NaN | NaN | NaN | WEST 35 STREET | HENRY HUDSON RIVER | NaN | ... | NaN | NaN | NaN | NaN | 4675769 | Sedan | NaN | NaN | NaN | NaN |
| 8 | 04/26/2023 | 22:20 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 61 Ed Koch queensborough bridge | ... | NaN | NaN | NaN | NaN | 4623865 | Sedan | Pick-up Truck | NaN | NaN | NaN |
| 9 | 09/11/2021 | 9:35 | BROOKLYN | 11208 | 40.667202 | -73.866500 | (40.667202, -73.8665) | NaN | NaN | 1211 LORING AVENUE | ... | NaN | NaN | NaN | NaN | 4456314 | Sedan | NaN | NaN | NaN | NaN |
| 10 | 12/14/2021 | 8:13 | BROOKLYN | 11233 | 40.683304 | -73.917274 | (40.683304, -73.917274) | SARATOGA AVENUE | DECATUR STREET | NaN | ... | NaN | NaN | NaN | NaN | 4486609 | NaN | NaN | NaN | NaN | NaN |
| 11 | 04/14/2021 | 12:47 | NaN | NaN | NaN | NaN | NaN | MAJOR DEEGAN EXPRESSWAY RAMP | NaN | NaN | ... | Unspecified | NaN | NaN | NaN | 4407458 | Dump | Sedan | NaN | NaN | NaN |
| 12 | 12/14/2021 | 17:05 | NaN | NaN | 40.709183 | -73.956825 | (40.709183, -73.956825) | BROOKLYN QUEENS EXPRESSWAY | NaN | NaN | ... | Unspecified | NaN | NaN | NaN | 4486555 | Sedan | Tractor Truck Diesel | NaN | NaN | NaN |
| 13 | 12/14/2021 | 8:17 | BRONX | 10475 | 40.868160 | -73.831480 | (40.86816, -73.83148) | NaN | NaN | 344 BAYCHESTER AVENUE | ... | Unspecified | NaN | NaN | NaN | 4486660 | Sedan | Sedan | NaN | NaN | NaN |
| 14 | 12/14/2021 | 21:10 | BROOKLYN | 11207 | 40.671720 | -73.897100 | (40.67172, -73.8971) | NaN | NaN | 2047 PITKIN AVENUE | ... | Unspecified | NaN | NaN | NaN | 4487074 | Sedan | NaN | NaN | NaN | NaN |
| 15 | 12/14/2021 | 14:58 | MANHATTAN | 10017 | 40.751440 | -73.973970 | (40.75144, -73.97397) | 3 AVENUE | EAST 43 STREET | NaN | ... | Unspecified | NaN | NaN | NaN | 4486519 | Sedan | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN |
| 16 | 12/13/2021 | 0:34 | NaN | NaN | 40.701275 | -73.888870 | (40.701275, -73.88887) | MYRTLE AVENUE | NaN | NaN | ... | Unspecified | NaN | NaN | NaN | 4486934 | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN | NaN |
| 17 | 12/14/2021 | 16:50 | QUEENS | 11413 | 40.675884 | -73.755770 | (40.675884, -73.75577) | SPRINGFIELD BOULEVARD | EAST GATE PLAZA | NaN | ... | Unspecified | NaN | NaN | NaN | 4487127 | Sedan | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN |
| 18 | 12/14/2021 | 8:30 | NaN | NaN | NaN | NaN | NaN | broadway | west 80 street -west 81 street | NaN | ... | Unspecified | NaN | NaN | NaN | 4486634 | Station Wagon/Sport Utility Vehicle | Sedan | NaN | NaN | NaN |
| 19 | 12/14/2021 | 0:59 | NaN | NaN | 40.596620 | -74.002310 | (40.59662, -74.00231) | BELT PARKWAY | NaN | NaN | ... | NaN | NaN | NaN | NaN | 4486564 | Sedan | NaN | NaN | NaN | NaN |
| 20 | 12/14/2021 | 23:10 | QUEENS | 11434 | 40.666840 | -73.789410 | (40.66684, -73.78941) | NORTH CONDUIT AVENUE | 150 STREET | NaN | ... | Unspecified | NaN | NaN | NaN | 4486635 | Sedan | Sedan | NaN | NaN | NaN |
| 21 | 12/14/2021 | 17:58 | BROOKLYN | 11217 | 40.681580 | -73.974630 | (40.68158, -73.97463) | NaN | NaN | 480 DEAN STREET | ... | Unspecified | NaN | NaN | NaN | 4486604 | Tanker | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN |
| 22 | 12/14/2021 | 20:03 | BROOKLYN | 11226 | 40.650680 | -73.958810 | (40.65068, -73.95881) | NaN | NaN | 878 FLATBUSH AVENUE | ... | NaN | NaN | NaN | NaN | 4486991 | Sedan | NaN | NaN | NaN | NaN |
| 23 | 12/14/2021 | 1:28 | NaN | NaN | NaN | NaN | NaN | MEEKER AVENUE | LORIMER STREET | NaN | ... | Unspecified | NaN | NaN | NaN | 4486284 | Station Wagon/Sport Utility Vehicle | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN |
| 24 | 12/11/2021 | 19:43 | BRONX | 10463 | 40.872620 | -73.904686 | (40.87262, -73.904686) | WEST KINGSBRIDGE ROAD | HEATH AVENUE | NaN | ... | Unspecified | NaN | NaN | NaN | 4487040 | Station Wagon/Sport Utility Vehicle | Sedan | NaN | NaN | NaN |
| 25 | 12/14/2021 | 14:30 | NaN | NaN | 40.783268 | -73.824850 | (40.783268, -73.82485) | WHITESTONE EXPRESSWAY | NaN | NaN | ... | Unspecified | Unspecified | NaN | NaN | 4486537 | Station Wagon/Sport Utility Vehicle | Sedan | Sedan | NaN | NaN |
| 26 | 12/11/2021 | 4:45 | MANHATTAN | 10001 | 40.748917 | -73.993546 | (40.748917, -73.993546) | NaN | NaN | 232 WEST 30 STREET | ... | Unspecified | NaN | NaN | NaN | 4486905 | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN | NaN |
| 27 | 12/14/2021 | 5:46 | NaN | NaN | 40.744644 | -73.770410 | (40.744644, -73.77041) | LONG ISLAND EXPRESSWAY | NaN | NaN | ... | Other Vehicular | NaN | NaN | NaN | 4487122 | Station Wagon/Sport Utility Vehicle | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN |
| 28 | 12/13/2021 | 6:30 | QUEENS | 11372 | 40.753730 | -73.885050 | (40.75373, -73.88505) | 82 STREET | 34 AVENUE | NaN | ... | NaN | NaN | NaN | NaN | 4486967 | Sedan | NaN | NaN | NaN | NaN |
| 29 | 12/14/2021 | 3:43 | NaN | NaN | 40.804375 | -73.937420 | (40.804375, -73.93742) | LEXINGTON AVENUE | NaN | NaN | ... | NaN | NaN | NaN | NaN | 4486304 | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN | NaN |
| 30 | 12/13/2021 | 17:40 | STATEN ISLAND | 10301 | 40.631650 | -74.087620 | (40.63165, -74.08762) | VICTORY BOULEVARD | WOODSTOCK AVENUE | NaN | ... | Unspecified | NaN | NaN | NaN | 4487001 | Sedan | Sedan | NaN | NaN | NaN |
| 31 | 12/14/2021 | 17:31 | BROOKLYN | 11230 | 40.623104 | -73.958090 | (40.623104, -73.95809) | EAST 18 STREET | AVENUE K | NaN | ... | NaN | NaN | NaN | NaN | 4486516 | Sedan | NaN | NaN | NaN | NaN |
| 32 | 12/14/2021 | 20:13 | BROOKLYN | 11215 | 40.665760 | -73.984500 | (40.66576, -73.9845) | NaN | NaN | 366 12 STREET | ... | Unspecified | NaN | NaN | NaN | 4486605 | Sedan | NaN | NaN | NaN | NaN |
| 33 | 12/14/2021 | 12:54 | BROOKLYN | 11217 | 40.687534 | -73.977500 | (40.687534, -73.9775) | FULTON STREET | SAINT FELIX STREET | NaN | ... | Unspecified | NaN | NaN | NaN | 4487052 | Sedan | Bike | NaN | NaN | NaN |
| 34 | 12/14/2021 | 17:15 | BROOKLYN | 11211 | 40.710957 | -73.951126 | (40.710957, -73.951126) | GRAND STREET | UNION AVENUE | NaN | ... | Unspecified | NaN | NaN | NaN | 4486556 | Bus | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN |
| 35 | 12/14/2021 | 22:49 | BRONX | 10455 | 40.818130 | -73.910126 | (40.81813, -73.910126) | NaN | NaN | 713 EAGLE AVENUE | ... | Unspecified | NaN | NaN | NaN | 4486875 | Taxi | NaN | NaN | NaN | NaN |
| 36 | 12/12/2021 | 9:00 | QUEENS | 11385 | 40.704470 | -73.901480 | (40.70447, -73.90148) | NaN | NaN | 59-14 67 AVENUE | ... | Unspecified | NaN | NaN | NaN | 4486933 | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN | NaN |
| 37 | 12/14/2021 | 16:25 | NaN | NaN | 40.784615 | -73.953964 | (40.784615, -73.953964) | EAST 93 STREET | NaN | NaN | ... | Driver Inattention/Distraction | NaN | NaN | NaN | 4486581 | Van | Bike | NaN | NaN | NaN |
| 38 | 11/02/2023 | 9:20 | NaN | NaN | NaN | NaN | NaN | 35 AVENUE | NaN | NaN | ... | NaN | NaN | NaN | NaN | 4675877 | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN | NaN |
| 39 | 04/14/2021 | 14:30 | NaN | NaN | NaN | NaN | NaN | EASTCHESTER ROAD | PELHAM PARKWAY NORTH | NaN | ... | Unspecified | NaN | NaN | NaN | 4407520 | Bus | NaN | NaN | NaN | NaN |
| 40 | 12/16/2021 | 6:59 | NaN | NaN | NaN | NaN | NaN | KINGSLAND AVENUE | MEEKER AVENUE | NaN | ... | NaN | NaN | NaN | NaN | 4486960 | NaN | NaN | NaN | NaN | NaN |
| 41 | 04/27/2023 | 15:40 | NaN | NaN | NaN | NaN | NaN | WILLIAMSBURG BRIDGE OUTER ROADWA | NaN | NaN | ... | Unspecified | NaN | NaN | NaN | 4624078 | Sedan | Sedan | NaN | NaN | NaN |
| 42 | 09/22/2022 | 16:16 | QUEENS | 11418 | 40.698257 | -73.826320 | (40.698257, -73.82632) | 123 STREET | 89 AVENUE | NaN | ... | Unspecified | NaN | NaN | NaN | 4566408 | Sedan | E-Scooter | NaN | NaN | NaN |
| 43 | 01/12/2023 | 21:00 | BROOKLYN | 11208 | NaN | NaN | NaN | NaN | NaN | 97-16 DREW STREET | ... | Unspecified | NaN | NaN | NaN | 4598234 | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN | NaN |
| 44 | 06/29/2022 | 16:00 | NaN | NaN | NaN | NaN | NaN | WILLIAMSBURG BRIDGE OUTER ROADWA | NaN | NaN | ... | Unspecified | NaN | NaN | NaN | 4542336 | Motorscooter | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN |
| 45 | 04/15/2021 | 16:15 | NaN | NaN | NaN | NaN | NaN | HUTCHINSON RIVER PARKWAY | NaN | NaN | ... | NaN | NaN | NaN | NaN | 4407665 | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN | NaN |
| 46 | 07/07/2021 | 11:42 | NaN | NaN | NaN | NaN | NaN | THROGS NECK BRIDGE | NaN | NaN | ... | Unspecified | NaN | NaN | NaN | 4456591 | Sedan | Sedan | NaN | NaN | NaN |
| 47 | 04/27/2023 | 22:13 | NaN | NaN | NaN | NaN | NaN | PELHAM PARKWAY SOUTH | WILLIAMSBRIDGE ROAD | NaN | ... | Unspecified | NaN | NaN | NaN | 4624101 | Sedan | NaN | NaN | NaN | NaN |
| 48 | 07/12/2022 | 17:50 | BROOKLYN | 11225 | 40.663303 | -73.960490 | (40.663303, -73.96049) | NaN | NaN | 44 EMPIRE BOULEVARD | ... | Unspecified | NaN | NaN | NaN | 4545699 | Sedan | NaN | NaN | NaN | NaN |
| 49 | 03/23/2022 | 10:00 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 71 EAST DRIVE | ... | NaN | NaN | NaN | NaN | 4512922 | Bike | NaN | NaN | NaN | NaN |
50 rows × 29 columns
3. Cleaned the data and Plotted
In [17]:
df2['CRASH DATE'] = pd.to_datetime(df['CRASH DATE'], errors='coerce')
In [18]:
df2_clean = df2.dropna(subset=['LATITUDE', 'LONGITUDE'])
In [11]:
plt.figure(figsize=(12,5))
df['VEHICLE TYPE CODE 1'].value_counts().head(15).plot(kind='bar')
plt.title("Top 15 Vehicle Types Involved in Collisions")
plt.xlabel("Vehicle Type")
plt.ylabel("Number of Collisions")
plt.show()
In [14]:
plt.figure(figsize=(12,5))
df.groupby(df['CRASH DATE'].dt.date).size().plot()
plt.title("Accidents Over Time")
plt.xlabel("Date")
plt.ylabel("Number of Accidents")
plt.show()
I tried my favourite data visualization - showing the crash/collision location on map. However I could not succeed.
In [20]:
# Convert LAT & LON to numeric values
df2['LATITUDE'] = pd.to_numeric(df['LATITUDE'], errors='coerce')
df2['LONGITUDE'] = pd.to_numeric(df['LONGITUDE'], errors='coerce')
# Remove missing or invalid values
df2_map = df2.dropna(subset=['LATITUDE', 'LONGITUDE'])
# Remove impossible coordinates (0,0)
df2_map = df2_map[(df2_map['LATITUDE'] != 0) & (df2_map['LONGITUDE'] != 0)]
In [ ]:
m = folium.Map(
location=[df2_map['LATITUDE'].mean(), df2_map['LONGITUDE'].mean()],
zoom_start=11,
tiles='OpenStreetMap'
)
for _, row in df2_map.iterrows():
folium.CircleMarker(
location=[row['LATITUDE'], row['LONGITUDE']],
radius=2,
color='red',
fill=True,
fill_opacity=0.5
).add_to(m)
m
In [ ]:
heat_data = df2_clean[['LATITUDE', 'LONGITUDE']].values.tolist()
m2 = folium.Map(
location=[df2_clean['LATITUDE'].mean(), df2_clean['LONGITUDE'].mean()],
zoom_start=11
)
HeatMap(heat_data, radius=6).add_to(m2)
m2
In [33]:
import folium
# Set starting point (center of NYC)
m = folium.Map(location=[40.7128, -74.0060], zoom_start=11)
# Add points
for _, row in df2.iterrows():
folium.CircleMarker(
location=[row["LATITUDE"], row["LONGITUDE"]],
radius=2,
color="red",
fill=True,
fill_opacity=0.6
).add_to(m)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[33], line 8 6 # Add points 7 for _, row in df2.iterrows(): ----> 8 folium.CircleMarker( 9 location=[row["LATITUDE"], row["LONGITUDE"]], 10 radius=2, 11 color="red", 12 fill=True, 13 fill_opacity=0.6 14 ).add_to(m) File /opt/conda/lib/python3.13/site-packages/folium/vector_layers.py:387, in CircleMarker.__init__(self, location, radius, popup, tooltip, **kwargs) 379 def __init__( 380 self, 381 location: Optional[Sequence[float]] = None, (...) 385 **kwargs: TypePathOptions, 386 ): --> 387 super().__init__(location, popup=popup, tooltip=tooltip) 388 self._name = "CircleMarker" 389 self.options = path_options(line=False, radius=radius, **kwargs) File /opt/conda/lib/python3.13/site-packages/folium/map.py:451, in Marker.__init__(self, location, popup, tooltip, icon, draggable, **kwargs) 449 super().__init__() 450 self._name = "Marker" --> 451 self.location = validate_location(location) if location is not None else None 452 self.options = remove_empty( 453 draggable=draggable or None, autoPan=draggable or None, **kwargs 454 ) 455 # this attribute is not used by Marker, but by GeoJson File /opt/conda/lib/python3.13/site-packages/folium/utilities.py:110, in validate_location(location) 105 raise ValueError( 106 "Location should consist of two numerical values, " 107 f"but {coord!r} of type {type(coord)} is not convertible to float." 108 ) 109 if math.isnan(float(coord)): --> 110 raise ValueError("Location values cannot contain NaNs.") 111 return [float(x) for x in coords] ValueError: Location values cannot contain NaNs.
In [34]:
m
Out[34]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Challenges I faced related to foliam
I wanted to mark the crash site on the map but I could not write the right code.
Mattermost Help



ChatGPT Help
In [1]:
import pandas as pd
import matplotlib.pyplot as plt
# Load your dataset
df = pd.read_csv("datasets/Motor_Vehicle_Collisions_-_Crashes.csv",low_memory=False)
# Drop rows with missing coordinates
df_geo = df.dropna(subset=["LATITUDE", "LONGITUDE"])
# Plot
plt.figure(figsize=(8, 10))
plt.scatter(df_geo["LONGITUDE"], df_geo["LATITUDE"], s=1)
plt.xlabel("Longitude")
plt.ylabel("Latitude")
plt.title("Crash Locations Scatter Plot")
plt.show(200)
In [9]:
import pandas as pd
import folium.map
from folium.plugins import HeatMap
df = pd.read_csv("datasets/Motor_Vehicle_Collisions_-_Crashes.csv")
df_geo = df.dropna(subset=["LATITUDE", "LONGITUDE"])
# Create base map centered on median coordinates
center_lat = df_geo["LATITUDE"].median()
center_lon = df_geo["LONGITUDE"].median()
m = folium.Map(location=[center_lat, center_lon], zoom_start=11)
# Prepare data for heatmap
heat_data = df_geo[["LATITUDE", "LONGITUDE"]].values.tolist()
HeatMap(heat_data, radius=8).add_to(m)
m
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) Cell In[9], line 2 1 import pandas as pd ----> 2 import folium.map 3 from folium.plugins import HeatMap 5 df = pd.read_csv("datasets/Motor_Vehicle_Collisions_-_Crashes.csv") ModuleNotFoundError: No module named 'folium'
Never Give UP!!¶
Tried again on 19th December, 2025. It was rewarding!¶
In [3]:
%pip install folium
import pandas as pd
import folium
Requirement already satisfied: folium in /opt/conda/lib/python3.13/site-packages (0.20.0) Requirement already satisfied: branca>=0.6.0 in /opt/conda/lib/python3.13/site-packages (from folium) (0.8.2) Requirement already satisfied: jinja2>=2.9 in /opt/conda/lib/python3.13/site-packages (from folium) (3.1.6) Requirement already satisfied: numpy in /opt/conda/lib/python3.13/site-packages (from folium) (2.3.3) Requirement already satisfied: requests in /opt/conda/lib/python3.13/site-packages (from folium) (2.32.5) Requirement already satisfied: xyzservices in /opt/conda/lib/python3.13/site-packages (from folium) (2025.4.0) Requirement already satisfied: MarkupSafe>=2.0 in /opt/conda/lib/python3.13/site-packages (from jinja2>=2.9->folium) (3.0.3) Requirement already satisfied: charset_normalizer<4,>=2 in /opt/conda/lib/python3.13/site-packages (from requests->folium) (3.4.4) Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.13/site-packages (from requests->folium) (3.11) Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.13/site-packages (from requests->folium) (2.5.0) Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.13/site-packages (from requests->folium) (2025.10.5) Note: you may need to restart the kernel to use updated packages.
In [4]:
df = pd.read_csv("datasets/Motor_Vehicle_Collisions_-_Crashes.csv", low_memory=False)
df.head(200)
Out[4]:
| CRASH DATE | CRASH TIME | BOROUGH | ZIP CODE | LATITUDE | LONGITUDE | LOCATION | ON STREET NAME | CROSS STREET NAME | OFF STREET NAME | ... | CONTRIBUTING FACTOR VEHICLE 2 | CONTRIBUTING FACTOR VEHICLE 3 | CONTRIBUTING FACTOR VEHICLE 4 | CONTRIBUTING FACTOR VEHICLE 5 | COLLISION_ID | VEHICLE TYPE CODE 1 | VEHICLE TYPE CODE 2 | VEHICLE TYPE CODE 3 | VEHICLE TYPE CODE 4 | VEHICLE TYPE CODE 5 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 09/11/2021 | 2:39 | NaN | NaN | NaN | NaN | NaN | WHITESTONE EXPRESSWAY | 20 AVENUE | NaN | ... | Unspecified | NaN | NaN | NaN | 4455765 | Sedan | Sedan | NaN | NaN | NaN |
| 1 | 03/26/2022 | 11:45 | NaN | NaN | NaN | NaN | NaN | QUEENSBORO BRIDGE UPPER | NaN | NaN | ... | NaN | NaN | NaN | NaN | 4513547 | Sedan | NaN | NaN | NaN | NaN |
| 2 | 11/01/2023 | 1:29 | BROOKLYN | 11230 | 40.621790 | -73.970024 | (40.62179, -73.970024) | OCEAN PARKWAY | AVENUE K | NaN | ... | Unspecified | Unspecified | NaN | NaN | 4675373 | Moped | Sedan | Sedan | NaN | NaN |
| 3 | 06/29/2022 | 6:55 | NaN | NaN | NaN | NaN | NaN | THROGS NECK BRIDGE | NaN | NaN | ... | Unspecified | NaN | NaN | NaN | 4541903 | Sedan | Pick-up Truck | NaN | NaN | NaN |
| 4 | 09/21/2022 | 13:21 | NaN | NaN | NaN | NaN | NaN | BROOKLYN BRIDGE | NaN | NaN | ... | Unspecified | NaN | NaN | NaN | 4566131 | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 195 | 03/23/2022 | 21:00 | QUEENS | 11354 | 40.776764 | -73.848015 | (40.776764, -73.848015) | NaN | NaN | 25-03 120 STREET | ... | NaN | NaN | NaN | NaN | 4514334 | Sedan | NaN | NaN | NaN | NaN |
| 196 | 03/26/2022 | 12:00 | QUEENS | 11377 | 40.747498 | -73.913090 | (40.747498, -73.91309) | NaN | NaN | 39-34 51 STREET | ... | NaN | NaN | NaN | NaN | 4513548 | Sedan | NaN | NaN | NaN | NaN |
| 197 | 03/26/2022 | 13:28 | BROOKLYN | 11206 | 40.701637 | -73.942276 | (40.701637, -73.942276) | GRAHAM AVENUE | DEBEVOISE STREET | NaN | ... | Unspecified | NaN | NaN | NaN | 4514046 | Station Wagon/Sport Utility Vehicle | Station Wagon/Sport Utility Vehicle | NaN | NaN | NaN |
| 198 | 03/26/2022 | 19:12 | QUEENS | 11419 | 40.693490 | -73.826546 | (40.69349, -73.826546) | NaN | NaN | 94-39 120 STREET | ... | Unspecified | NaN | NaN | NaN | 4513581 | Station Wagon/Sport Utility Vehicle | Sedan | NaN | NaN | NaN |
| 199 | 03/26/2022 | 21:29 | QUEENS | 11374 | 40.726463 | -73.859430 | (40.726463, -73.85943) | WETHEROLE STREET | 65 ROAD | NaN | ... | NaN | NaN | NaN | NaN | 4513772 | Bike | NaN | NaN | NaN | NaN |
200 rows × 29 columns
In [5]:
df = df[['LATITUDE', 'LONGITUDE', 'CRASH DATE', 'VEHICLE TYPE CODE 1']]
df = df.dropna(subset=['LATITUDE', 'LONGITUDE'])
df.head(200)
Out[5]:
| LATITUDE | LONGITUDE | CRASH DATE | VEHICLE TYPE CODE 1 | |
|---|---|---|---|---|
| 2 | 40.621790 | -73.970024 | 11/01/2023 | Moped |
| 9 | 40.667202 | -73.866500 | 09/11/2021 | Sedan |
| 10 | 40.683304 | -73.917274 | 12/14/2021 | NaN |
| 12 | 40.709183 | -73.956825 | 12/14/2021 | Sedan |
| 13 | 40.868160 | -73.831480 | 12/14/2021 | Sedan |
| ... | ... | ... | ... | ... |
| 226 | 40.761040 | -73.881620 | 12/10/2021 | Sedan |
| 227 | 40.669777 | -73.958300 | 12/10/2021 | Station Wagon/Sport Utility Vehicle |
| 228 | 40.839443 | -73.904360 | 12/10/2021 | Ambulance |
| 229 | 40.752450 | -73.945070 | 12/10/2021 | Station Wagon/Sport Utility Vehicle |
| 230 | 40.687748 | -73.980125 | 12/08/2021 | Station Wagon/Sport Utility Vehicle |
200 rows × 4 columns
In [6]:
center_lat = df['LATITUDE'].mean()
center_lon = df['LONGITUDE'].mean()
m = folium.Map(
location=[center_lat, center_lon],
zoom_start=11,
tiles='OpenStreetMap'
)
In [7]:
for _, row in df.sample(1000).iterrows(): # limit to 1000 for performance
folium.CircleMarker(
location=[row['LATITUDE'], row['LONGITUDE']],
radius=0.5,
color='red',
fill=True,
fill_opacity=0.6,
popup=f"Date: {row['CRASH DATE']}<br>Vehicle: {row['VEHICLE TYPE CODE 1']}"
).add_to(m)
In [8]:
m
Out[8]:
Make this Notebook Trusted to load map: File -> Trust Notebook