简体   繁体   English

熊猫:检查行的值是否相似

[英]Pandas: Check if row has similar values

I'm generating an overlay for a map using pandas and used: 我正在使用熊猫生成地图的叠加层并使用:

if ((df['latitude'] == new_latitude) & (df['longitude'] == new_longitude)).any():
   continue

to make sure that I don't produce duplicate points. 确保我不会产生重复的分数。 But I am starting to produce points that are 0.001 different (in either longitude, latitude or both) than one already produced. 但是我开始产生的点与已经产生的点相差0.001(经度,纬度或两者)。 How can I prevent this in a similar manner as above? 如何以与上述类似的方式防止这种情况?

IIUC you can subtract from the entire series and then just filter the points: 您可以从整个序列中减去IIUC,然后仅过滤点:

thresh = 0.001
lat = df.loc[(df['latitude'] - new_latitude).abs() > thresh, 'latitude']
lon = df.loc[(df['longtitude'] - new_longtitude).abs() > thresh, 'longtitude']

this uses abs to get the absolute value to generate a boolean mask and filter all the duplicate and near duplicate values out. 这使用abs来获取绝对值,以生成布尔掩码并过滤掉所有重复的值和接近重复的值。

You could use numpy.isclose function with atol setted to your precision: 您可以将numpy.isclose函数与atol设置为您的精度:

import numpy as np
prec = 0.001
np.isclose(df['latitude'], new_latitude, atol=prec)

if ((np.isclose(df['latitude'], new_latitude, prec) & (np.isclose(df['longitude'], new_longitude, prec)).any():
   continue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM