user286076
user286076

Reputation: 151

Converting degree minute second (DMS) columns to decimal degrees in pandas dataframe

I have a data frame like the below and would like to convert the Latitude and Longitude columns in Degree, Minute, Second format into decimal degrees and want the updated table with other column

any help would be appreciated

enter image description here

Upvotes: 0

Views: 537

Answers (1)

swatchai
swatchai

Reputation: 18782

Here is the relevant code that uses apply, lambda to process each row of the dataframe and creates a new column lat_decimal to contain the result.

# Create dataframe
d6 = {'id':['a1','a2','a3'], 
      'lat_deg': [10, 11, 12],
      'lat_min': [15, 30, 45],
      'lat_sec': [10, 20, 30]
    }
df6 = pd.DataFrame( data=d6 )

df6["lat_decimal"] = df6[["lat_deg","lat_min","lat_sec"]].apply(lambda row: row.values[0] + row.values[1]/60 + row.values[2]/3600, axis=1)

The resulting dataframe:-

   id  lat_deg  lat_min  lat_sec  lat_decimal
0  a1       10       15       10    10.252778
1  a2       11       30       20    11.505556
2  a3       12       45       30    12.758333

Upvotes: 0

Related Questions