Home > database >  Latitude and Longitude from Pgeocode object to dataframe
Latitude and Longitude from Pgeocode object to dataframe

Time:04-14

import pandas as pd
import numpy as np
import pgeocode

d = {'Trust': ["Airedale NHS","Alder Hay NHS", "Ashford and st peters"], 'Postcode': ["BD20 6TD","L12 2AP", "KT16 0PZ"]}
df = pd.DataFrame(data=d)
df

nomi=  pgeocode.Nominatim('GB')
nomi.query_postal_code("bD20")

I have a df of organisation names and postcodes, I want to add lat and long columns. With pgeocode I can create an object that give lat and long and other values for the postcode given. But I not clear how run this function for all the postcodes in the df and to add only the lat and long it returns to the df.

Pgeocode

CodePudding user response:

You can use apply on rows

df[['latitude', 'longitude']] = df.apply(lambda row: nomi.query_postal_code(row['Postcode'])[['latitude', 'longitude']], axis=1, result_type='expand')
                   Trust  Postcode  latitude  longitude
0           Airedale NHS  BD20 6TD   53.9322  -1.978708
1          Alder Hay NHS   L12 2AP   53.4328  -2.909600
2  Ashford and st peters  KT16 0PZ   51.3775  -0.541500
  • Related