如何使x(m)半径相对于python坐标?

2024-04-26 14:24:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据库,包含,消耗和坐标,例如:

enter image description here

我需要写一个代码,让我能够从一个地方到另一个地方,找出在100m半径内小于平均消耗量的消耗量。它的目的是不计算100m半径一次,而是计算每对坐标。你知道吗

代码:

R9 = []
R9_NaN = []

for index, row in df.iterrows():
    coord_1 = (row['X'], row['Y'])
    for index, row2 in df.iterrows():
        coord_2 = (row['X'], row['Y'])
        if coord_2 != coord_1:
            dist = geopy.distance.geodesic(coord_1, coord_2).km
            if dist <= 0.100:
                média=sum((row['Consumo2018']/12)/(len(coord_2)+1))
                if row['Consumo2018']/12 < 1.5*média:
                    R9_NaN.append(index)
                    R9.append(0)
                else:
                    R9.append(0)

print(R9)

你知道吗地理距离是已计算两个坐标之间距离的库。你知道吗

在上述代码中,“média”被假定为100米范围内场地的平均消耗量,各地也应有所不同。你知道吗

它给了我这个错误:

TypeError: 'float' object is not iterable


Tags: 代码indfforindexif地方半径
1条回答
网友
1楼 · 发布于 2024-04-26 14:24:02

所以你得到了一个浮点错误,因为你试图取一个数的和。这可以通过删除sum行中的media = ...来解决。如果希望这一行取整个数据帧的平均值,则需要更改该行以从数据帧而不仅仅是行中获取数据。你知道吗

R9 = []
R9_NaN = []

for index, row in df.iterrows():
    coord_1 = (row['X'], row['Y'])
    for index, row2 in df.iterrows():
        coord_2 = (row['X'], row['Y'])
        if coord_2 != coord_1:
            dist = geopy.distance.geodesic(coord_1, coord_2).km
            if dist <= 0.100:
#                média=((row['Consumo2018']/12)/(len(coord_2)+1)) #Change to this to remove sum
                média=((df['Consumo2018']/12)/(len(df['Consumo2018'])+1)) #Change to this to get actual mean
                if row['Consumo2018']/12 < 1.5*média:
                    R9_NaN.append(index)
                    R9.append(0)
                else:
                    R9.append(0)

print(R9)

相关问题 更多 >