[英]For loop error, SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
I tried to count spots that are in 100meter radius with GPS coordinates.我试图用 GPS 坐标计算半径 100 米内的点。 My data has 4 columns like below;我的数据有 4 列,如下所示;
Index Longitude Latitude Count
1 35.897654 26.568987 0
2 32.98717 23.897740 0
3 36.23245 34.243246 0
. .... .... ....
. .... .... ....
I calculated distance from coordinates with Haversine method.我用Haversine方法计算了与坐标的距离。 I described it as a function.我把它描述为一个函数。
haversine([x1,y1],[x2,y2]) gives the meter between GPS coordinates. hasrsine([x1,y1],[x2,y2]) 给出 GPS 坐标之间的米。
My problem occurs in below code;我的问题出现在下面的代码中;
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
it raises this error;它引发了这个错误;
main :5: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame main :5: SettingWithCopyWarning: 试图在 DataFrame 的切片副本上设置一个值
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy请参阅文档中的注意事项: http : //pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
I checked the document but I couldn't find something useful or I didn't understand it.我检查了文档,但我找不到有用的东西或者我不理解它。
What I am doing wrong?我做错了什么? What's the proper way to do this nested loop operation?执行此嵌套循环操作的正确方法是什么?
Thanks in advance.提前致谢。
cr.iloc[x][2]=cr.iloc[x][2]+1
this code doesn't set the value in dataframe此代码未在数据框中设置值
I've changed to;我已经改为;
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)
By looking at your question, I didn't get an answer but my research to use .set_value lead me to something very similar.通过查看您的问题,我没有得到答案,但我对使用 .set_value 的研究使我得出了非常相似的结论。
I'm using .at[index, 'column'] = value
我正在使用.at[index, 'column'] = value
My code :我的代码:
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.')
Link to further information :链接到更多信息:
https://www.dataindependent.com/pandas/pandas-set-values/ https://www.dataindependent.com/pandas/pandas-set-values/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.