I have a list of locations. For every location in the location column, there is a function which finds its coordinates, if they are not there already. This operation is performed for all. The loop copies the last value of latitudes and longitudes in all the rows, which it shouldn't do. Where am I making the mistake?
What I have
location gpslat gpslong
Brandenburger Tor na na
India Gate na na
Gateway of India na na
What I want
location gpslat gpslong
Brandenburger Tor 52.16 13.37
India Gate 28.61 77.22
Gateway of India 18.92 72.81
What I get
location gpslat gpslong
Brandenburger Tor 18.92 72.81
India Gate 18.92 72.81
Gateway of India 18.92 72.81
My Code
i = 0
for location in df.location_clean:
try:
if np.isnan(float(df.gpslat.iloc[i])) == True:
df.gpslat.iloc[i], df.gpslong.iloc[i] = find_coordinates(location)
print(i, "Coordinates Found for --->", df.location_clean.iloc[i])
else:
print(i,'Coordinates Already Present')
except:
print('Spelling Mistake Encountered at', df.location_clean.iloc[i], 'Moving to NEXT row')
continue
i = i + 1
I guess, I am making a logical error either with the index i or the statement df.gpslat.iloc[i], df.gpslong.iloc[i] = find_coordinates(location) . I tried changing them and rerunning the loop, but it's same. It's also a time consuming process as there are thousands of locations.
dfinto your question