问题描述
我对数据帧(df)中的每一行应用了以下函数,并使用Lat0 / Lon0,方向(方位角)和距离(dist)以“度/秒”获得了新的Lat / Lon:>
def location(row):
from geopy.distance import distance
dist = row['dist']
direction = row['azimuth']
lat0 = 20
lon0 = 20
return distance(kilometers=dist).destination((lat0,lon0),direction)
df['coordinate'] = df.apply(lambda row: location(row),axis=1)
我想知道是否有一种方法可以基于df ['coordinate']输出在数据框中使用纬度和经度(“十进制度”)信息来创建新的两列。
Out[]:
time zenith azimuth o3 uncertainty flag \
61 2019-01-24 15:02:57.983999999 66.90 121.72 241.85 4.9131 1
62 2019-01-24 15:04:35.616000000 66.57 121.94 227.36 4.1773 1
63 2019-01-24 15:06:13.248000000 66.25 122.16 232.97 3.4649 1
64 2019-01-24 15:07:50.880000000 65.92 122.39 236.81 3.1841 1
dist coordinate
61 51.578278 19 45m 16.3524s N,20 25m 6.9961s E
62 50.766056 19 45m 24.9176s N,20 24m 39.7557s E
63 49.998803 19 45m 32.885s N,20 24m 13.9121s E
64 49.227710 19 45m 40.8577s N,20 23m 47.8847s E
更新 解决了
def location(row,lat0,lon0):
from geopy.distance import distance
dist = row['dist']
direction = row['azimuth']
return distance(kilometers=dist).destination((lat0,direction).format_decimal()
df['coordinate'] = df.apply(lambda row: location(row,20,20),axis=1)
split_data = df.coordinate.str.split(',')
df['lat'] = split_data.apply(lambda x: x[0])
df['long'] = split_data.apply(lambda x: x[1])
当前数据框:
Out[]:
time zenith azimuth o3 uncertainty flag \
61 2019-01-24 15:02:57.983999999 66.90 121.72 241.85 4.9131 1
62 2019-01-24 15:04:35.616000000 66.57 121.94 227.36 4.1773 1
63 2019-01-24 15:06:13.248000000 66.25 122.16 232.97 3.4649 1
64 2019-01-24 15:07:50.880000000 65.92 122.39 236.81 3.1841 1
dist coordinate lat \
61 51.578278 19.75454233221212,20.41861002686191 19.75454233221212
62 50.766056 19.756921547635606,20.411043240303318 19.756921547635606
63 49.998803 19.759134724013204,20.403864475793757 19.759134724013204
64 49.227710 19.761349364643046,20.396634631525913 19.761349364643046
long
61 20.41861002686191
62 20.411043240303318
63 20.403864475793757
64 20.396634631525913
解决方法
如果删除.format_decimal()
,则location
函数将返回Point
实例而不是字符串(请参阅https://geopy.readthedocs.io/en/stable/#geopy.point.Point),其中十进制坐标可以很容易地提取为属性:
df['point'] = df.apply(lambda row: location(row,20,20),axis=1)
df['lat'] = df['point'].apply(lambda point: point.latitude)
df['long'] = df['point'].apply(lambda point: point.longitude)