How to use geopy vicenty distance over dataframe columns?

2024/9/21 14:49:22

I have a dataframe with location column which contains lat,long location as follows

 deviceid                             location        
1102ADb75        [12.9404578177, 77.5548244743]

How to get the distance between consecutive rows using geopy's vicenty function? I tried following code

from geopy.distance import vincenty 
vincenty(df['location'].shift(-1), df['location']).miles

It returns following error - TypeError: __new__() takes at most 4 arguments (5 given)

EDIT - where df is a Pandas dataframe containing deviceId & Location columns as shown above Also

print type(df)
class 'pandas.core.frame.DataFrame'
Answer

Based on geopy's github you should pass two tuples to the vincenty function:

    >>> from geopy.distance import vincenty>>> point_a = (41.49008, -71.312796)>>> point_b = (41.499498, -81.695391)>>> print(vincenty(point_a, point_b).miles)538.3904451566326

EDIT:

import pandas as pd
from geopy.distance import vincentydata = [[101, [41.49008, -71.312796]],[202, [41.499498, -81.695391]]]
df = pd.DataFrame(data=data, columns=['deviceid', 'location'])print df
>>>    deviceid                 location
>>> 0       101   [41.49008, -71.312796]
>>> 1       202  [41.499498, -81.695391]print vincenty(df['location'][0], df['location'][1]).miles
>>> 538.390445157
https://en.xdnf.cn/q/72052.html

Related Q&A

Opening a postgres connection in psycopg2 causes python to crash

Im getting the following error message when I try to open up a connection to a postgres database. Perhaps its related to OpenSSL, but I cant understand the error message. Can anyone help?>>>…

Calling generated `__init__` in custom `__init__` override on dataclass

Currently I have something like this: @dataclass(frozen=True) class MyClass:a: strb: strc: strd: Dict[str, str]...which is all well and good except dicts are mutable, so I cant use my class to key anot…

Python Watchdog process existing files on startup

I have a simple Watchdog and Queue process to monitor files in a directory. Code taken from https://camcairns.github.io/python/2017/09/06/python_watchdog_jobs_queue.htmlimport time from watchdog.events…

Updating Text In Entry (Tkinter)

The piece of code below takes input from user through a form and then returns the input as multiplied by 2. What I want to do is, when a user types a number (for example 5) and presses the "Enter&…

Python prevent overflow errors while handling large floating point numbers and integers

I am working on a python program to calculate numbers in the Fibonacci sequence. Here is my code:import math def F(n):return ((1+math.sqrt(5))**n-(1-math.sqrt(5))**n)/(2**n*math.sqrt(5)) def fib(n):for…

Python selenium sending keys into textarea

Im using Python 3.4.4 to access a website (https://readability-score.com/) that has a textarea, which dynamically updates when new values are added. Im trying to input a string into that textarea box b…

how to run several executable using python?

I have an executable under linux. I have an 8 core processor. I want to run 8 different instances of the same executable with different arguments.I tried os.system("process_name args")It does…

How to retrieve only arabic texts from a string using regular expression?

I have a string which has both Arabic and English sentences. What I want is to extract Arabic Sentences only.my_string=""" What is the reason ذَلِكَ الْكِتَابُ لَا رَ…

Formatted output in OpenOffice/Microsoft Word with Python

I am working on a project (in Python) that needs formatted, editable output. Since the end-user isnt going to be technically proficient, the output needs to be in a word processor editable format. The …

Issue in calling Python code from Java (without using jython)

I found this as one of the ways to run (using exec() method) python script from java. I have one simple print statement in python file. However, my program is doing nothing when I run it. It neither pr…