对于循环错误,设置与复制警告:正在尝试在数据帧中的切片副本上设置值



我试图用GPS坐标计算半径100米内的点。我的数据有 4 列,如下所示;

Index     Longitude    Latitude      Count
1         35.897654    26.568987       0
2         32.98717     23.897740       0
3         36.23245     34.243246       0
.          ....         ....          ....
.          ....         ....          ....

我用哈弗辛方法计算了坐标的距离。我将其描述为一个函数。

haversine([x1,y1],[x2,y2]( 给出了 GPS 坐标之间的仪表。

我的问题出现在下面的代码中;

for x in range(0,25486):
for y in range(1,25486):
a = haversine([cr.iloc[x][0],cr.iloc[x][1]],[cr.iloc[y][0],cr.iloc[y][1]])
if a <= 100 and a > 0:
cr.iloc[x][2]=cr.iloc[x][2]+1

它引发了此错误;

main:5:设置复制警告:正在尝试在数据帧中的切片副本上设置值

请参阅文档中的注意事项:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

我检查了文档,但我找不到有用的东西,或者我不明白它。

我做错了什么? 执行此嵌套循环操作的正确方法是什么?

提前谢谢。

cr.iloc[x][2]=cr.iloc[x][2]+1

此代码不会在数据帧中设置值

我已更改为;

for x in range(0,25486):
t=0
for y in range(0,25486):
a = haversine([cr.iloc[x][1],cr.iloc[x][2]],[cr.iloc[y][1],cr.iloc[y][2]])
if a <= 400 and a > 0:
t = t+1   
cr.set_value(x,'Adet',t)

通过查看您的问题,我没有得到答案,但我的研究.set_value引导我得出非常相似的东西。

我正在使用.at[index, 'column'] = value

我的代码 :

for index, row in customersOnMap.iterrows():
x = row.loc['customer_zip_code_prefix']
if x in list_zip_code.geolocation_zip_code_prefix.values:
lat = list_zip_code[list_zip_code.geolocation_zip_code_prefix == x].geolocation_lat.values[0]
long = list_zip_code[list_zip_code.geolocation_zip_code_prefix == x].geolocation_lng.values[0]
customersOnMap.at[index, 'geolocation_lat'] = lat
customersOnMap.at[index, 'geolocation_lng'] = long
else:
print('Couldn t find the zip code', x, 'in the list.')

链接到更多信息 :

https://www.dataindependent.com/pandas/pandas-set-values/

最新更新