0

I'm trying to run this code:

for x in range(len(df10)):
    try:
        time.sleep(1) #to add delay in case of large DFs
        geocode_result = gmaps.geocode(df10['Address'][x])
        df10['lat'][x] = geocode_result[0]['geometry']['location'] ['lat']
        df10['long'][x] = geocode_result[0]['geometry']['location']['lng']
    except IndexError:
        print("Address was wrong...")
    except Exception as e:
        print("Unexpected error occurred.", e )

I would like the for loop to iterate over a list of addresses, which is now stored in the column of a pandas dataframe called df10['Address'], then apply Google geocoding service to extract the longitude and latitude for each row and save these as columns of the original dataframe.

When I try to do this, I get the following error:

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

I understand this is because I'm trying to overwrite on the original dataframe, but I'm really struggling to find an alternative code that works.

Hopefully someone can help!

user3119334
  • 41
  • 1
  • 3
  • try with `loc/iloc` accessor.For detailed information have a look on: https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas – Anurag Dabas May 21 '21 at 12:50
  • `df10.iloc[x, 'lat'] = ...`. I'm using `iloc` here, since you loop over the length of `df10`, but this will be a problem with the column label. You may be better off looping over the index of `df10`, or doing something with `.apply()`, or trying to avoid the loop altogether. – 9769953 May 21 '21 at 12:52

2 Answers2

1

Make new columns by applying a function that returns multiple values.

Fake data:

import pandas as pd
df = pd.DataFrame({'a':[1,2,3,4,5,6],'b':list('abcdef')})

>>> df
   a  b
0  1  a
1  2  b
2  3  c
3  4  d
4  5  e
5  6  f

Construct a function that takes a row for its argument and operates on one of the columns.

def f(row):
    lat = row['a'] * 2
    lon = row['a'] % 2
    return lat,lon

Apply the function to the DataFrame and assign the result to the new columns.

>>> df[['lat','lon']] = df.apply(f,axis=1,result_type='expand')
>>> df
   a  b  lat  lon
0  1  a    2    1
1  2  b    4    0
2  3  c    6    1
3  4  d    8    0
4  5  e   10    1
5  6  f   12    0
>>>

The expand argument turns the list-like result from the function into columns.

You didn't provide any example data and I don't have gmap installed but I imagine your code should look like this:

def g(row):
    g_result = gmaps.geocode(row['Address'])
    lat = g_result[0]['geometry']['location'] ['lat']
    lon = g_result[0]['geometry']['location']['lng']
    return lat,lon

and used like this:

df[['lat','lon']] = df.apply(g,axis=1,result_type='expand')
wwii
  • 23,232
  • 7
  • 37
  • 77
0

Assuming df10 isn't itself a slice of another DataFrame:

df10.loc[df10.index[x], 'lat'] = geocode_result[0]['geometry']['location'] ['lat']
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65