TypeError:float() 参数必须是字符串或数字,而不是"元组"



不会执行。 需要找到纬度和经度之间的距离。

divvy['Start Coordinates'] = (zip(divvy['from_station_latitude'], divvy['from_station_longitude']))
divvy['End Coordinates'] = (zip(divvy['to_station_latitude'], divvy['to_station_longitude']))
from geopy.distance import geodesic
dist = []
for i in range(len(divvy)):
    dist.append(vincenty(divvy.iloc[i]['Start Coordinates'], divvy.iloc[i]['End Coordinates']).miles)
    if (i%1000000==0):
        print(i)

我认为"divvy"是一个pandas DataFrame对象。关于您的代码有两个注释:

    您可能误解了"zip"和"()",
  1. 正确的用法是list(zip(stuff,stuff)),因为这会返回一个要保存在"divvy"中的列表。实际上,当前代码将迭代器的副本保存在"divvy"而不是值中。
  2. 计算
  3. "divvy"中所有条目的距离可能比逐个计算距离更快

例如:1行法

dist = [vincenty((start_x, start_y), (end_x, end_y)).miles 
        for start_x, start_y, end_x, end_y in zip(divvy['from_station_latitude'],
                                                  divvy['from_station_longitude'],
                                                  divvy['to_station_latitude'],
                                                  divvy['to_station_longitude'])]

您的方法:

divvy['Start Coordinates'] = list(zip(divvy['from_station_latitude'], divvy['from_station_longitude']))
divvy['End Coordinates'] = list(zip(divvy['to_station_latitude'], divvy['to_station_longitude']))

from geopy.distance import geodesic
dist = []
for i in range(len(divvy)):
    dist.append(vincenty(divvy.iloc[i]['Start Coordinates'],divvy.iloc[i]['End Coordinates']).miles)
    if (i%1000000==0):
        print(i)

最新更新