Skip to content

Instantly share code, notes, and snippets.

@rozanecm
Last active August 2, 2023 14:31
Show Gist options
  • Save rozanecm/29926a7c8132a0a25e3b12a24abdff86 to your computer and use it in GitHub Desktop.
Save rozanecm/29926a7c8132a0a25e3b12a24abdff86 to your computer and use it in GitHub Desktop.
Distance between geographic centers of all US states
state latitude longitude
Alabama 32.7794 -86.8287
Alaska 64.0685 -152.2782
Arizona 34.2744 -111.6602
Arkansas 34.8938 -92.4426
California 37.1841 -119.4696
Colorado 38.9972 -105.5478
Connecticut 41.6219 -72.7273
Delaware 38.9896 -75.5050
District of Columbia 38.9101 -77.0147
Florida 28.6305 -82.4497
Georgia 32.6415 -83.4426
Hawaii 20.2927 -156.3737
Idaho 44.3509 -114.6130
Illinois 40.0417 -89.1965
Indiana 39.8942 -86.2816
Iowa 42.0751 -93.4960
Kansas 38.4937 -98.3804
Kentucky 37.5347 -85.3021
Louisiana 31.0689 -91.9968
Maine 45.3695 -69.2428
Maryland 39.0550 -76.7909
Massachusetts 42.2596 -71.8083
Michigan 44.3467 -85.4102
Minnesota 46.2807 -94.3053
Mississippi 32.7364 -89.6678
Missouri 38.3566 -92.4580
Montana 47.0527 -109.6333
Nebraska 41.5378 -99.7951
Nevada 39.3289 -116.6312
New Hampshire 43.6805 -71.5811
New Jersey 40.1907 -74.6728
New Mexico 34.4071 -106.1126
New York 42.9538 -75.5268
North Carolina 35.5557 -79.3877
North Dakota 47.4501 -100.4659
Ohio 40.2862 -82.7937
Oklahoma 35.5889 -97.4943
Oregon 43.9336 -120.5583
Pennsylvania 40.8781 -77.7996
Rhode Island 41.6762 -71.5562
South Carolina 33.9169 -80.8964
South Dakota 44.4443 -100.2263
Tennessee 35.8580 -86.3505
Texas 31.4757 -99.3312
Utah 39.3055 -111.6703
Vermont 44.0687 -72.6658
Virginia 37.5215 -78.8537
Washington 47.3826 -120.4472
West Virginia 38.6409 -80.6227
Wisconsin 44.6243 -89.9941
Wyoming 42.9957 -107.5512
import pandas as pd
import scipy.spatial
# load csv with states' data.
# this csv is included in this gist.
# Data taken from https://en.wikipedia.org/wiki/List_of_geographic_centers_of_the_United_States#Updated_list_of_geographic_centers
# Coordinates correspond to Updated geographic centers of the states of the United States and Washington, DC
statesDF = pd.read_csv('states.csv');
# create distances dataframe
distancesDF = pd.DataFrame(columns=["state1", "state2", "distance"])
# Line 22 can be modified to calculate whichever distance is preferred.
# See available options at
# https://docs.scipy.org/doc/scipy/reference/spatial.distance.html#module-scipy.spatial.distance
for state1, lat1, lon1 in zip(statesDF.state, statesDF.latitude, statesDF.longitude):
for state2, lat2, lon2 in zip(statesDF.state, statesDF.latitude, statesDF.longitude):
distancesDF = distancesDF.append({
"state1": state1,
"state2": state2,
"distance": scipy.spatial.distance.euclidean([float(lat1), float(lon1)], [float(lat2), float(lon2)])
}, ignore_index=True);
distancesDF.to_csv('state_distances.csv', index=False);
@rozanecm
Copy link
Author

rozanecm commented Nov 3, 2017

NOTE: In the list of states figures the District of Columbia, which is not a state.

@jbaughma
Copy link

For DC, the signs are flipped. It should be: 38.9101, -77.0147

@rozanecm
Copy link
Author

@jbaughma Just updated, thanks for the comment!

@luna4289
Copy link

What is the unit of the output?

@rebecca-sich
Copy link

I think line 22 is calculating Euclidean distance in a way it shouldn't (to @luna4289 's point above, there are no units to calculating the distance on a plane this way from lat and long coordinates). I have edited the code in a way that I think works better - feel free to checkout my fork: https://gist.github.com/rebecca-sich/1c422acc1b222b8a04fcd3cfae79bc18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment