2

I am trying to create 2 columns in my dataframe for Longitude and Latitude which I want to find by using my address column called 'Details'.

I have tried from

geopy.extra.rate_limiter import RateLimiter

locator=Nominatim(user_agent="MyGeocoder")

results['location']=results['Details'].apply

results['point']=results['location'].apply(lambda loc:tuple(loc['point']) if loc else None)
results[['latitude', 'longitude',]]=pd.DataFrame(results['point'].tolist(), index=results.index) 

But this gives the error "method object is not subscriptable"

I want to create a loop to get all coordinates for each address

Details Sale Price  Post Code   Year Sold
1   53 Eastbury Grove, London, W4 2JT Flat, Lease...    450000.0    W4  2020
2   Flat 148 Wedgwood House Lambeth Walk, London, ...   325000.0    E11 2020
3   63 Russell Road, Wimbledon, London, SW19 1QN ...    800000.0    W19 2020
4   Flat 2 9 Queens Gate Place, London, SW7 5NX F...    400000.0    W7  2020
5   83 Chingford Mount Road, London, E4 8LU Freeh...    182000.0    E4  2020
... ... ... ... ...
47  702 Rutherford Heights Rodney Road, London, SE...   554750.0    E17 2015
48  Flat 48 Highlands Court Highland Road, London,...   340000.0    E19 2015
49  5 Mount Nod Road, London, SW16 2LQ Flat, Leas...    395000.0    W16 2015
50  6 Woodmill Street, London, SE16 3GG Terraced,...    1010000.0   E16 2015
51  402 Rutherford Heights Rodney Road, London, SE...   403200.0    E17 2015
300 rows × 4 columns
bigbounty
  • 16,526
  • 5
  • 37
  • 65
AmyZK
  • 21
  • 2
  • Details Sale Price Post Code Year Sold 1 53 Eastbury Grove, London, W4 2JT Flat, Lease... 450000.0 W4 2020 2 Flat 148 Wedgwood House Lambeth Walk, London, ... 325000.0 E11 2020 3 63 Russell Road, Wimbledon, London, SW19 1QN ... 800000.0 W19 2020 4 Flat 2 9 Queens Gate Place, London, SW7 5NX F... 400000.0 W7 2020 5 83 Chingford Mount Road, London, E4 8LU Freeh... 182000.0 E4 2020 ... ... ... ... ... 47 702 Rutherford Heights Rodney Road, London, SE... 554750.0 E17 2015 48 Flat 48 Highlands Court Highland Road, London,... 340000.0 E19 2015 300 rows × 4 columns – AmyZK Jul 30 '20 at 11:16
  • Please post the data in the question – bigbounty Jul 30 '20 at 11:17

1 Answers1

0

Try this

import pandas as pd
import geopandas
import geopy
from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter

locator = Nominatim(user_agent="myGeocoder")
geocode = RateLimiter(locator.geocode, min_delay_seconds=1)

def lat_long(row):
    loc = locator.geocode(row["Details"])
    row["latitude"] = loc.latitude
    row["longitude"] = loc.longitude
    return row

results.apply(lat_long, axis=1)
bigbounty
  • 16,526
  • 5
  • 37
  • 65
  • I get the following error:~/conda/envs/python/lib/python3.6/site-packages/pandas/core/apply.py in series_generator(self) 386 # of it. Kids: don't do this at home. 387 ser = self.obj._ixs(0, axis=0) --> 388 mgr = ser._mgr 389 blk = mgr.blocks[0] 390 AttributeError: 'Series' object has no attribute '_mgr' – AmyZK Jul 30 '20 at 11:52
  • I have updated the answer. Can you try the updated answerr? – bigbounty Jul 30 '20 at 11:56
  • Same error. I changed it to (row['Details']) in the first attempt too – AmyZK Jul 30 '20 at 11:59