Here is a vectorized approach, that also uses matrix
* vector
([1, 1./60, 1./3600]
) multiplication:
In [233]: %paste
def dms2dec(s):
x = (s.str.upper()
.str.split(r'[°\'"]', expand=True)
.replace(['S','W','N','E'], [-1,-1,1,1])
.astype('float'))
return x.iloc[:, :3].dot([1, 1./60, 1./3600]).mul(x.iloc[:, 3])
## -- End pasted text --
In [234]: df[['Latitude','Longitude']] = df[['Latitude','Longitude']].apply(dms2dec)
In [235]: df
Out[235]:
Parent Company CPO PKO Latitude Longitude
0 Incasi Raya X -0.865636 101.446192
1 Incasi Raya X -1.394247 101.591792
2 Incasi Raya X 0.332397 99.382322
3 Incasi Raya X 0.362753 99.633244
4 Incasi Raya X -1.685156 102.235467
5 Incasi Raya X -1.250592 101.575106
6 Incasi Raya X -2.328961 100.992931
7 Musim Mas X 1.748872 101.371094
step by step explanation:
In [239]: x = (s.str.upper()
...: .str.split(r'[°\'"]', expand=True)
...: .replace(['S','W','N','E'], [-1,-1,1,1])
...: .astype('float'))
In [240]: x
Out[240]:
0 1 2 3
0 0.0 51.0 56.29 -1.0
1 1.0 23.0 39.29 -1.0
2 0.0 19.0 56.63 1.0
3 0.0 21.0 45.91 1.0
4 1.0 41.0 6.56 -1.0
5 1.0 15.0 2.13 -1.0
6 2.0 19.0 44.26 -1.0
7 1.0 44.0 55.94 1.0
In [241]: x.iloc[:, :3].dot([1, 1./60, 1./3600])
Out[241]:
0 0.865636
1 1.394247
2 0.332397
3 0.362753
4 1.685156
5 1.250592
6 2.328961
7 1.748872
dtype: float64
In [242]: x.iloc[:, :3].dot([1, 1./60, 1./3600]).mul(x.iloc[:, 3])
Out[242]:
0 -0.865636
1 -1.394247
2 0.332397
3 0.362753
4 -1.685156
5 -1.250592
6 -2.328961
7 1.748872
dtype: float64