0

I am trying to use this technique Calculate distance between two coordinates for a fixed point in a DataFrame

   from typing import Tuple
import geopy.distance


def distance(
    lat: float, lon: float, fixed_coords: Tuple[float] = (36.7196, -4.42002)
) -> float:
    return geopy.distance.distance((lat, lon), fixed_coords).km


x = dataframe.apply(lambda row: distance(row[lat], row[lon],axis =1))

error:

---> 11 x = dataframe.apply(lambda row: distance(row[lat], row[lon],axis =1))

NameError: name 'lat' is not defined

1 Answers1

0

The name error is becasue the interpreter thinks lat and lon are variable names, not the name of columns. Try using strings instead.

x = dataframe.apply(lambda row: distance(row["lat"], row["lon"],axis =1))
JRose
  • 1,382
  • 2
  • 5
  • 18
  • KeyError: 'lat' – Avneesh Chaudhary Nov 10 '22 at 18:44
  • What is the datatype of row? Are you sure that "lat" and "lon" are present in row? – JRose Nov 10 '22 at 18:55
  • they are column names in the dataframe containing longitude and latitude data as int64 – Avneesh Chaudhary Nov 10 '22 at 19:02
  • If you are getting a `KeyError` that means "lat" and "lon" are not keys in the dataframe. You should verify all of the column names are what you expect, I recommend using something like this: https://stackoverflow.com/questions/49188960/how-to-show-all-columns-names-on-a-large-pandas-dataframe – JRose Nov 10 '22 at 19:22