Reputation: 18790
I have a pandas DataFrame with 3 columns and milion rows:
time longitude latitude
1 x1 y1
2 x2 y2
3 x3 y3
...
I want to apply a function to calculate distance based on the longitude and latitude. Basically I need a way to express the function can handle two adjacent rows in dataframe like
compute_distance(x1,y1,x2,y2)
I know there are ways to apply function along axis 1 and 0, but they seem only apply to single row or column. How can I express something involving several rows or columns.
Upvotes: 1
Views: 2003
Reputation: 2617
Apply won't be able to this, but you can do something simple like the following:
def compute_distance(df):
next_df = df.shift(-1)
return distance_on_unit_sphere(df["lat"], df["long"],
next_df["lat"], next_df["long"]):
from here http://www.johndcook.com/python_longitude_latitude.html
Upvotes: 1