Home > Blockchain >  Convert latitude and longitude to float
Convert latitude and longitude to float

Time:11-01

I have a dataframe df like these 3 first rows are these

City Longitude Latitude
Toluca W099.6569 N19.2925
Pune E73.8562 N18.5167
Bloemfontain E026.216667 S29.116667

For example Bloemfontain row should be like these at the end

City Longitude Latitude
Bloemfontain -29.116667 26.216667

I used this code

pattern = r'(?P<d>[\d\.] ).*?(?P<m>[\d\.] ).*?(?P<s>[\d\.] )'
dms = df['Latitude'].str.extract(pattern).astype(float)
df['LATITUDE'] = dms['d']   dms['m'].div(60)   dms['s'].div(3600)
dms = df['Longitude'].str.extract(pattern).astype(float)
df['LONGITUDE'] = dms['d']   dms['m'].div(60)   dms['s'].div(3600)

but got not the right longitude and latitude

CodePudding user response:

I don't see where it is necessary to utilize a pattern search at all. Here is how I would do the task.

# function to perform the partitioning
def cnvrtCoord(val: str) -> float:
    sgn =  1
    if val[0] == 'S' or val[0] == 'W':
        sgn = -1
    return float(val[1:]) * sgn  

Then to change the contents of the Longitude and Latitude columns use:

df['Longitude'] = [cnvrtCoord(x) for x in df['Longitude'].tolist()]
df['Latitude'] = [cnvrtCoord(x) for x in df['Latitude'].tolist()]  

This would produce the dataframe shown below given your input sample:

    City    Longitude   Latitude
0   Toluca  -99.656900  19.292500
1   Pune    73.856200   18.516700
2   Bloemfontain    26.216667   -29.116667
  • Related