How do I convert vincenty distance to float in pandas dataframe

Nabih Bawazir

I have problem to analyse vincenty distance because the format is object and have km metrics in there, I want to analyse further. I want to convert vincenty distance to float format

Here's the data

customer_id lat_free    long_free   lat_device  long_device radius      timestamp
7509        -6.283468   106.857636  -7.802388   110.368660  1264.000000 2017-12-14 21:18:40.327
7509        -6.283468   106.857636  -7.804296   110.367192  14.000000   2017-12-15 20:02:21.923

Here's my code

from geopy.distance import vincenty
df['Vincenty_distance'] = df.apply(lambda x: vincenty((x['lat_free'], x['long_free']), (x['lat_device'], x['long_device'])), axis = 1)

This is the result

customer_id lat_free    long_free   lat_device  long_device radius      timestamp                 Vincenty_distance
7509        -6.283468   106.857636  -7.802388   110.368660  1264.000000 2017-12-14 21:18:40.327   422.7123873310482 km
7509        -6.283468   106.857636  -7.804296   110.367192  14.000000   2017-12-15 20:02:21.923   422.64674499172787 km

I need to convert Vincenty_distance to float

jezrael

The best is add .km:

df['Vincenty_distance'] = df.apply(lambda x: vincenty((x['lat_free'], x['long_free']), (x['lat_device'], x['long_device'])).km, axis = 1)

Or use after processing - convert to string, remove last letters and cast to floats:

df['Vincenty_distance'] = df['Vincenty_distance'].astype(str).str[:-3].astype(float)

print (df)
   customer_id  lat_free   long_free  lat_device  long_device  radius  \
0         7509 -6.283468  106.857636   -7.802388   110.368660  1264.0   
1         7509 -6.283468  106.857636   -7.804296   110.367192    14.0   

                 timestamp  Vincenty_distance  
0  2017-12-14 21:18:40.327         422.712361  
1  2017-12-15 20:02:21.923         422.646709  

print (df.dtypes)

customer_id            int64
lat_free             float64
long_free            float64
lat_device           float64
long_device          float64
radius               float64
timestamp             object
Vincenty_distance    float64
dtype: object

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert integer 'category' dtypes in a Pandas DataFrame to 'int64'/'float64'?

In pandas, how do I convert a series of float or none to strings with integers

How do I convert '$ -' from a string to a float using pandas?

How do I convert a pandas series which is multidimensional to pandas dataframe

how to convert string to float in pandas dataframe

How do i convert python list into pandas dataframe with predefined columns

How do I convert a numpy array into a pandas dataframe?

How do I convert timestamp to datetime.date in pandas dataframe?

How do I convert a text table to a pandas dataframe?

how do i convert a numpy array to pandas dataframe

How do I efficiently convert pandas dataframe to image array?

how do i convert from a webcomponent to pandas dataframe

How do I calculate the Levenshtein distance between two Pandas DataFrame columns?

How do convert an entire column string to a float within a dataframe using pandas?

How do I convert a Pandas Dataframe with one column into a Pandas Dataframe of two columns?

How do I convert an float to an integer in Elixir

How do I convert a variable to float?

How do I convert RGB to float values?

How do i convert string percentage into float

Dataframe columns have numbers many of them in str format and many in float, how do I convert all of them to float

How do I convert a float datatype column, displayed in e notation format, to integer in Pandas?

How to convert 'float64' to timestamp in pandas dataframe

Python: Pandas Dataframe How to Convert timedelta column to float column

how to convert pandas dataframe to numpy array of float data type

How to convert string ('"12,488"') to float in pandas dataframe?

How to convert a column with object dtype to float in a pandas dataframe?

How do I sort a dataframe by distance from zero value

How do I get the distance to a previous occurrence of a value in Polars dataframe?

How do I convert currencies to dollars in a dataframe