Converting pandas data frame with degree minute second (DMS) coordinates to decimal degrees
Basing my answer on a function from SO you can do it like this:
Interestingly this answer is also 2x as fast as MaxU and Amis answer for a dataset with +500 rows. My bet is that the bottleneck is str.extract(). But something is clearly strange.
import pandas as pd
import re
#https://stackoverflow.com/questions/33997361
def dms2dd(s):
# example: s = """0°51'56.29"S"""
degrees, minutes, seconds, direction = re.split('[°\'"]+', s)
dd = float(degrees) + float(minutes)/60 + float(seconds)/(60*60);
if direction in ('S','W'):
dd*= -1
return dd
df = pd.DataFrame({'CPO': {0: 'Raya', 1: 'Raya'},
'Latitude': {0: '0°51\'56.29"S', 1: '1°23\'39.29"S'},
'Longitude': {0: '101°26\'46.29"E', 1: '101°35\'30.45"E'},
'PKO': {0: 'X', 1: 'X'},
'ParentCompany': {0: 'Incasi', 1: 'Incasi'}})
df['Latitude'] = df['Latitude'].apply(dms2dd)
df['Longitude'] = df['Longitude'].apply(dms2dd)
printing df returns:
CPO Latitude Longitude PKO ParentCompany
0 Raya -0.865636 101.446192 X Incasi
1 Raya -1.394247 101.591792 X Incasi
Update: To correct your mistake you could do something in the lines of:
m = df['Latitude'].str[-2] != '"'
df.loc[m, 'Latitude'] = df.loc[m, 'Latitude'].str[:-1] + '"' + df.loc[m, 'Latitude'].str[-1]
Full example:
import re
s1 = """0°51'56.29"S"""
s2 = """0°51'56.29S"""
df = pd.Series((s1,s2)).to_frame(name='Latitude')
m = df['Latitude'].str[-2] != '"'
df.loc[m, 'Latitude'] = df.loc[m, 'Latitude'].str[:-1] + '"' + df.loc[m, 'Latitude'].str[-1]
print(df)
You can use vectorized operations using pd.Series.str.extract
. For the latitude, for example:
parts = df.Latitude.str.extract('(\d+)°(\d+)\'([^"]+)"([N|S|E|W])', expand=True)
>>> (parts[0].astype(int) + parts[1].astype(float) / 60 + parts[2].astype(float) / 3600) * parts[3].map({'N':1, 'S':-1, 'E': 1, 'W':-1})
0 101.446192
1 101.591792
2 99.382322
3 99.633244
4 102.235467
5 101.575106
6 100.992931
7 101.371094