0

I am new to using pandas and I'm attempting to fix the last portion of my script. When calling the following function I am getting the error:

ValueError: Cannot set a DataFrame with multiple columns to the single column place_name
def get_place_name(latitude, longitude):
    location = geolocator.reverse(f"{latitude}, {longitude}", exactly_one=True)
    if location is None:
        return None
    else:
        return location.address

This function is being called from here:

metadata_df = extract_metadata(dir_path)
print("[+] Metadata extracted from all image files")
print(metadata_df.columns)
geolocator = Nominatim(user_agent="exif_location")
metadata_df['place_name'] = metadata_df.apply(
    lambda row: get_place_name(
        row['gps_latitude'], row['gps_longitude']),
    axis=1)

Any help or suggestions would be much appreciated :)

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Please make a [mre] including enough code to reproduce the error (and only that much) and the [full error message with traceback](https://meta.stackoverflow.com/q/359146/4518341). For specifics, see [How to make good reproducible pandas examples](/q/20109391/4518341). – wjandrea Apr 10 '23 at 14:55

1 Answers1

1

Your code works well for me (Pandas 1.5.3, Geopy 2.3.0):

df = pd.DataFrame({'lat': [34.053691, 40.712728],
                   'lon': [-118.242766, -74.006015]})

geolocator = Nominatim(user_agent='MyApp')

df['place_name'] = df.apply(lambda x: get_place_name(x['lat'], x['lon']), axis=1)
print(df)

Output

         lat         lon                                         place_name
0  34.053691 -118.242766  Los Angeles City Hall, 200, North Spring Stree...
1  40.712728  -74.006015  New York City Hall, 260, Broadway, Lower Manha...

I don't understand why your function try to return a dataframe with multiple columns. Your error is reproducible if you do something like:

df['place_name'] = df[['lat', 'lon']]
...
ValueError: Cannot set a DataFrame with multiple columns to the single column place_name
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Corralien
  • 109,409
  • 8
  • 28
  • 52