拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 在资料框中找到最接近的值?

在资料框中找到最接近的值?

白鹭 - 2022-03-09 1992 0 0

假设我有一个如下所示的资料框,

     0           1               2               3               4
0   (989, 998)  (1074, 999)     (1159, 1000)    (1244, 1001)    (1329, 1002)
1   (970, 1042) (1057, 1043)    (1143, 1044)    (1230, 1045)    (1316, 1046)
2   (951, 1088) (1039, 1089)    (1127, 1090)    (1214, 1091)    (1302, 1092)
3   (930, 1137) (1020, 1138)    (1109, 1139)    (1198, 1140)    (1287, 1141)
4   (909, 1188) (1000, 1189)    (1091, 1190)    (1181, 1191)    (1271, 1192)

每个单元格在元组中有 x 和 y 坐标。我有一个名为 I 的输入,它也是元组中的 x 和 Y 坐标。我的目标是找到输入 I 的最近点。

样本输入:

(1080, 1000)

示例输出:

(1074, 999)

我试过下面的代码片段。

def find_nearest(array, key):
    min_ = 1000
    a = 0
    b = 0
    for item in array:
        diff = abs(item[0]-key[0]) abs(item[1]-key[1])
        if diff<min_:
            min_ = diff
            a,b = item
        if diff==0:
            return (a,b)
    return (a,b)
find_nearest(sum(df.values.tolist(), []), I)

这给了我我所期望的。但是,对于这个问题有什么有效的解决方案吗?

uj5u.com热心网友回复:

尝试:

# Setup
data = [[(989, 998), (1074, 999), (1159, 1000), (1244, 1001), (1329, 1002)],
        [(970, 1042), (1057, 1043), (1143, 1044), (1230, 1045), (1316, 1046)],
        [(951, 1088), (1039, 1089), (1127, 1090), (1214, 1091), (1302, 1092)],
        [(930, 1137), (1020, 1138), (1109, 1139), (1198, 1140), (1287, 1141)],
        [(909, 1188), (1000, 1189), (1091, 1190), (1181, 1191), (1271, 1192)]]
df = pd.DataFrame(data)

l = (1080, 1000)

out = min(df.to_numpy().flatten(), key=lambda c: (c[0]- l[0])**2   (c[1]-l[1])**2)
print(out)

# Output:
(1074, 999)

更新

dist = df.stack().apply(lambda c: (c[0]- l[0])**2   (c[1]-l[1])**2)
idx = dist.index[dist.argmin()]
val = df.loc[idx]

print(idx)
print(val)

# Output:
(0, 1)
(1074, 999)

更新 2

arr = df.to_numpy().astype([('x', int), ('y', int)])
dist = (arr['x'] - l[0])**2   (arr['y'] - l[1])**2
idx = tuple(np.argwhere(dist == np.min(dist))[0])
val = arr[idx]  # or df.loc[idx]

uj5u.com热心网友回复:

我写的这个片段怎么样?

# cordinates: np.ndarray(n, 2)
def find_nearest(cordinates, x, y):
    x_d = np.abs(cordinate[:, 0] - x)
    y_d = np.abs(cordinate[:, 1] - y)
    nearest_idx = np.argmin(x_d    y_d)
    return cordinate[nearest_idx]

uj5u.com热心网友回复:

您可以使用 swifter 和 applymap 进行更快的处理

I = (1080, 1000)

diff = df.swifter.applymap(lambda item: abs(item[0]-I[0]) abs(item[1]-I[1]))

col_index = diff.min(axis=0)[diff.min(axis=0) == diff.min(axis=0).min()].index[0]
row_index = diff.min(axis=1)[diff.min(axis=1) == diff.min(axis=1).min()].index[0]

df.loc[row_index, col_index]

在资料框中找到最接近的值?

uj5u.com热心网友回复:

看起来您只需要一个两列的 DataFrame 并找到每行和样本坐标之间的距离。所以这是我的实作:

您的资料在复制时以字符串形式出现。你实际上并不需要这一行:

data = pd.Series(df.to_numpy().flatten()).str.strip().str.strip('()').str.split(',', expand=True).astype(int)
sample = (1080, 1000)

解决方案从这里开始:

distances = data.apply(lambda x: (x[0]-sample[0])**2 (x[1]-sample[1])**2, axis=1)
out = tuple(data[distances == distances.min()].to_numpy()[0])

输出:

(1074, 999)

uj5u.com热心网友回复:

您可以使用在资料框中找到最接近的值?

更新 2

但是,在较大的 Dataframe 中,它开始下拉:

在资料框中找到最接近的值?

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *