删除有其他要求的重复项

时间:2019-11-12 19:18:28

标签: numpy duplicates unique

我有三列msg_f = ProtoField.string("mydissector.msg", "msg") local getMsg = buffer(13) -- starting on byte 13 local msg = getMsg:le_ustring() subtree:add(msg_f, getMsg, msg) ,其中(x,y,m)x是坐标,y是度量。有一些重复项,它们定义为相同的m。然后,在这些重复项中,我按度量(x,y)对它们进行排名,我只选择一个最小值为m的重复项之一。这是一个示例:

m

存在三个具有相同坐标x = np.array([1,1,2,2,1,1,2]) y = np.array([1,2,1,2,1,1,1]) m = np.array([10,2,13,4,6,15,7]) 的副本,其中三个(1,1)的最小值为6。有两个具有相同坐标m的副本,其中两个的最小值{ {1}}是7。所以我想要的最终结果是:

(2,1)

m无法处理这种情况。有什么好主意吗?

2 个答案:

答案 0 :(得分:1)

您可以尝试以下操作:

import collections
x = np.array([1,1,2,2,1,1,2])
y = np.array([1,2,1,2,1,1,1])
m = np.array([10,2,13,4,6,15,7])
coords = [str(x[i]) + ',' + str(y[i]) for i in range(len(x))]
results = collections.OrderedDict()
for coords, m in zip(coords, m):
    if coords not in results:
        results[coords] = m
    else:
        if m < results[coords]:
            results[coords] = m
x = np.array([int(key.split(',')[0]) for key, _ in results.items()])
y = np.array([int(key.split(',')[1]) for key, _ in results.items()])
m = np.array([value for _, value in results.items()])

答案 1 :(得分:1)

我们可以在这里使用熊猫作为更清洁的解决方案-

import pandas as pd

In [43]: df = pd.DataFrame({'x':x,'y':y,'m':m})

In [46]: out_df = df.iloc[df.groupby(['x','y'])['m'].idxmin()]

# Format #1 : Final output as a 2D array
In [47]: out_df.values
Out[47]: 
array([[1, 1, 6],
       [1, 2, 2],
       [2, 1, 7],
       [2, 2, 4]])

# Format #2 : Final output as three separate 1D arrays
In [50]: X,Y,M = out_df.values.T

In [51]: X
Out[51]: array([1, 1, 2, 2])

In [52]: Y
Out[52]: array([1, 2, 1, 2])

In [53]: M
Out[53]: array([6, 2, 7, 4])