1
votes

I am trying to create a new column to manipulate the data set:

df['longitude'] = df['longitude'].astype(float)
df['latitude'] = df['latitude'].astype(float)

then ran the function for haversine: from math import radians, cos, sin, asin, sqrt

def haversine(lon1,lat1,lat2,lon2):
    """
    Calculate the great circle distance between two points 
    on the earth (specified in decimal degrees)
    """
    # convert decimal degrees to radians 
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    # haversine formula 
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 
    km = 6367 * c
    return km

But when I run this code :

df['d_centre']=haversine(lon1,
                         lat1,
                         df.longitude.astype(float),
                         df.latitude.astype(float))

to create a new column in my df I get this error:

Error: cannot convert the series to <class 'float'>

I tried this as well:

df['d_centre']= haversine(lon1,lat1,lat2,lon2)

the haversine is working but when I try to create the new column in my df, I get this error. I have tried converting to a list as well but I'm getting the same result

1
use np.sin instead of math.sinQuang Hoang
I tried that as well, i believe my function is working fine but when I try to create a new column 'd_centre' is is stating series cannot be converted to class floatDhanush Ram

1 Answers

0
votes

I figured out the answer: have to use numpy for all the math and write the code for the new column with the df

from math import radians, cos, sin, asin, sqrt def haversine_np(lon1,lat1,lon2,lat2): """ Calculate the great circle distance between two points on the earth (specified in decimal degrees) """ # convert decimal degrees to radians lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2]) # haversine formula dlon = lon2 - lon1 dlat = lat2 - lat1 a = np.sin(dlat/2)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2)**2 c = 2 * np.arcsin(np.sqrt(a)) km = 6367 * c return km

Create a new column: df2['d_centre'] =haversine_np(df2['lon1'],df2['lat1'],df2['lon2'],df2['lat2'])