Python: Trouble running any additional queries on Geopy output?

Question:

have encountered the following – Struggling to wrap my head around it.

Have some data that looks like this:

enter image description here

I’ve written the following Python that works out the distance between the 2 sets of coordinates:

from geopy import distance

# Calculate distance between 2 sets of coordinates
# Result is float64

data['Distance'] = data[['Start_Lat', 'Start_Lng', 'End_Lat', 'End_Lng']].apply(lambda x: distance.distance((x[0],x[1]), (x[2],x[3])).km, axis=1)
print(data['Distance'])
# Create quantiles

data["DisBucket"] = pd.qcut(df_nyc.Aftermath, q=[0, 0.3, 0.7, 1.0], labels=['LOW', 'MEDIUM', 'HIGH'])

The first bit works fine and returns the following as a float64:

enter image description here

The second bit however fails and returns the following:

enter image description here

It doesn’t seem to like the output from Geopy for whatever reason. I haven’t been able to work out away around this. Is there potentially a way to copy across the values without the association to Geopy?

Any advice would be greatly appreciated 🙂

Asked By: afroduck

||

Answers:

#Create sample data
dat = [[43.11944,-75.2932, 40.12029, -74.2935],[40.83488,-75.8662, 40.83377, -73.8633],[40.81212,-73.9165, 40.80491, -73.9112],
[43.07367,-78.9906, 43.07523, -78.9906],
[41.30884,-74.0253, 40.30746, -74.028]]
data = pd.DataFrame(dat, columns=[‘Start_lat’, ‘Start_Lng’, ‘End_Lat’, ‘End_Lng’])

#Calculate distance between 2 sets of coordinates
data[‘Distance’] = data.apply(lambda x: distance.distance((x[‘Start_lat’], x[‘Start_Lng’]), (x[‘End_Lat’], x[‘End_Lng’])).km, axis=1)

#Create quantile

data[‘DisBucket’] = pd.qcut(data.Distance, q=[0, 0.3, 0.7, 1.0], labels=[‘LOW’, ‘MEDIUM’, ‘HIGH’])

Answered By: kgbonnet
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.