Home > front end >  Lat/long data in colon 170:11:10 to convert in decimal points
Lat/long data in colon 170:11:10 to convert in decimal points

Time:08-07

How can I convert lat/long columns to decimal points?

1

CodePudding user response:

All it takes is a simple conversion function.

import pandas as pd

data = [
    ['26:20:25','66:00:41'],
    ['25:23:15','63:59:36'],
    ['59:44:00','18:55:00'],
    ['34:50:25','33:32:27'],
    ['34:53:06','33:34:18'],
    ['34:45:12','33:22:43'],
    ['34:29:32','71:30:20'],
    ['34:28:45','71:27:53'],
    ['45:00:01','-122:15:00']
]

df = pd.DataFrame( data, columns=['latitude','longitude'] )

def cvt(dms):
    parts = [int(k) for k in dms.split(':')]
    mmss = parts[1]/60.0 parts[2]/3600.0
    if parts[0] < 0:
        return parts[0]-mmss
    else:
        return parts[0] mmss

df['latf'] = df['latitude'].apply(cvt)
df['longf'] = df['longitude'].apply(cvt)
print(df)

Output

   latitude   longitude       latf       longf
0  26:20:25    66:00:41  26.340278   66.011389
1  25:23:15    63:59:36  25.387500   63.993333
2  59:44:00    18:55:00  59.733333   18.916667
3  34:50:25    33:32:27  34.840278   33.540833
4  34:53:06    33:34:18  34.885000   33.571667
5  34:45:12    33:22:43  34.753333   33.378611
6  34:29:32    71:30:20  34.492222   71.505556
7  34:28:45    71:27:53  34.479167   71.464722
8  45:00:01  -122:15:00  45.000278 -122.250000
  • Related