-1

I use the codes below to identify address from latitude and longitude:

import pandas
df = pandas.read_csv("/Users/Downloads/address.csv", encoding='utf-8')
df['address'] = ''

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="http")
df['address'] = df.apply(
    lambda row: geolocator.reverse((row['latitude'], row['longitude'])), axis=1)
county = df.raw['address']

The output is:

id latitude longitude address
1 40.0175444 -105.2833481 919, Pearl Street, Boulder, Boulder County, Colorado, 80302, United States
2 39.94700652 -82.997471 740, South High Street, Brewery District, Columbus, Franklin County, Ohio, 43206, United States

I want to create two columns that store the name of county and 5-digit FIPS code. The last code shows the error: AttributeError: 'DataFrame' object has no attribute 'raw'

  • To answer the question of why the error: You're trying to call `.raw` on the dataframe, not on the data stored in the dataframe. You need to use `apply()` again on the location data column to convert it to `raw()` and then you can index into the individual attributes as in [this answer](https://stackoverflow.com/a/40476209/7835267) – G. Anderson Jul 08 '21 at 14:49

1 Answers1

0

You will just need to do a string split on the address and retrieve the last and the second last index to get the country and the code.

But before you do that, you will have to convert your address to a string.

df[‘address’] = df[‘address’].map(str)

Then,

df[‘country’] = df[‘address’].apply(lambda x : x.split(“,”)[-1] )
df[‘code’] = df[‘address’].apply(lambda x : x.split(“,”)[-2])
Akash Dubey
  • 304
  • 1
  • 11