我正在尝试使用python按照答案here中的步骤计算时间排序坐标之间的距离和速度。在代码结尾处,我遇到一个错误,该错误表示尚未定义全局名称,但显然已经定义了。
这是我的数据示例
ID timestamp latitude longitude
0 72 20/01/2015 09:47 -6.646405565 71.35696828
1 72 20/01/2015 15:47 -6.642237759 71.36032005
2 72 20/01/2015 21:47 -6.639229675 71.36914769
3 73 21/01/2015 03:47 -6.648699053 71.37865551
4 73 21/01/2015 09:47 -6.65574147 71.37957366
5 74 21/01/2015 15:47 -6.660118996 71.37990588
6 74 21/01/2015 21:47 -6.666138734 71.38266541
到目前为止,我已经能够运行以下代码
import pandas as pd
df = pd.read_csv(filename)
df['timestamp'] = pd.to_datetime(df['timestamp'], format='%d/%m/%Y %H:%M')
from math import sin, cos, sqrt, atan2, radians
def getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2):
R = 6371 # Radius of the earth in km
dLat = radians(lat2-lat1)
dLon = radians(lon2-lon1)
rLat1 = radians(lat1)
rLat2 = radians(lat2)
a = sin(dLat/2) * sin(dLat/2) + cos(rLat1) * cos(rLat2) * sin(dLon/2) * sin(dLon/2)
c = 2 * atan2(sqrt(a), sqrt(1-a))
d = R * c # Distance in km
return d
def calc_velocity(dist_km, time_start, time_end):
"""Return 0 if time_start == time_end, avoid dividing by 0"""
return dist_km / (time_end - time_start).seconds if time_end > time_start else 0
# First sort by ID and timestamp:
df = df.sort_values(by=['ID', 'timestamp'])
# Group the sorted dataframe by ID, and grab the initial value for lat, lon, and time.
df['lat0'] = df.groupby('ID')['latitude'].transform(lambda x: x.iat[0])
df['lon0'] = df.groupby('ID')['longitude'].transform(lambda x: x.iat[0])
df['t0'] = df.groupby('ID')['timestamp'].transform(lambda x: x.iat[0])
# create a new column for distance
df['dist_km'] = df.apply(
lambda row: getDistanceFromLatLonInKm(
lat1=row['latitude'],
lon1=row['longitude'],
lat2=row['lat0'],
lon2=row['lon0']
),
axis=1
)
这时,我得到一个错误,暗示'getDistanceFromLatLonInKm'
尚未定义。下面是回溯和错误
Traceback (most recent call last):
File "<pyshell#36>", line 9, in <module>
axis=1
File "C:\Python27\ArcGIS10.6\lib\site-packages\pandas\core\frame.py", line 4061, in apply
return self._apply_standard(f, axis, reduce=reduce)
File "C:\Python27\ArcGIS10.6\lib\site-packages\pandas\core\frame.py", line 4157, in _apply_standard
results[i] = func(v)
File "<pyshell#36>", line 3, in <lambda>
lambda row: getDistanceFromLatLonInKm(
NameError: ("global name 'getDistanceFromLatLonInKm' is not defined", u'occurred at index 0')
此代码在哪里出错?