Skip to content

Instantly share code, notes, and snippets.

@alexkapps
Last active March 18, 2016 22:09
Show Gist options
  • Save alexkapps/f2c9fc40b4b31f9449fb to your computer and use it in GitHub Desktop.
Save alexkapps/f2c9fc40b4b31f9449fb to your computer and use it in GitHub Desktop.
#converts DMS lat/long format columns like '89444400','89444400' to decimal degrees
#Author: Alex Kappel
import pandas as pd
#assigns file location
input_path = '~/Documents/test.csv'
#imports csv
df = pd.read_csv(input_path, sep=',', encoding='utf-8')
#makes sure input is interpreted as string (for slicing)
df.latitude = df.latitude.astype(str)
df.longitude = df.longitude.astype(str)
#splits string into degree, min, sec columns
df['lat_degree'] = df['latitude'].str[0:2].astype(float)
df['lat_minute'] = df['latitude'].str[2:4].astype(float)
df['lat_second'] = df['latitude'].str[4:6].astype(float)
df['lon_degree'] = df['longitude'].str[0:2].astype(float)
df['lon_minute'] = df['longitude'].str[2:4].astype(float)
df['lon_second'] = df['longitude'].str[4:6].astype(float)
#conversion math
df['lat_decimal_degrees'] = df['lat_degree'] + ( df['lat_minute'] / 60 ) + ( df['lat_second'] / 3600 )
df['lon_decimal_degrees'] = df['lon_degree'] + ( df['lon_minute'] / 60 ) + ( df['lon_second'] / 3600 )
#removes 'working' columns
df = df.drop('lat_degree', 1)
df = df.drop('lat_minute', 1)
df = df.drop('lat_second', 1)
df = df.drop('lon_degree', 1)
df = df.drop('lon_minute', 1)
df = df.drop('lon_second', 1)
#outputs csv
df.to_csv('dd_output.csv', encoding='utf-8', index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment