在pandas.DataFrame的多列上使用numpy.unique [英] Using numpy.unique on multiple columns of a pandas.DataFrame

查看:1354
本文介绍了在pandas.DataFrame的多列上使用numpy.unique的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用numpy.unique来获取pandas.DataFrame的两列的反向唯一索引.

I am looking to use numpy.unique to obtain the reverse unique indexes of two columns of a pandas.DataFrame.

我知道如何在一列上使用它:

I know how to use it on one column:

u, rev = numpy.unique(df[col], return_inverse=True)

但是我想在多列上使用它.例如,如果df看起来像:

But I want to use it on multiple columns. For example, if the df looks like:

    0   1   
0   1   1
1   1   2
2   2   1
3   2   1
4   3   1

然后我想获取反向索引:

then I would like to get the reverse indexes:

[0,1,2,2,3]

推荐答案

方法1

这里是一种NumPy方法,将每行转换为标量,每行将每行都视为二维(用于2列数据)网格上的一个索引元组-

Here's one NumPy approach converting each row to a scalar each thinking of each row as one indexing tuple on a two-dimensional (for 2 columns of data) grid -

def unique_return_inverse_2D(a): # a is array
    a1D = a.dot(np.append((a.max(0)+1)[:0:-1].cumprod()[::-1],1))
    return np.unique(a1D, return_inverse=1)[1]

如果数据中有负数,我们也需要使用min来获得这些标量.因此,在这种情况下,请使用a.max(0) - a.min(0) + 1代替a.max(0) + 1.

If you have negative numbers in the data, we need to use min too to get those scalars. So, in that case, use a.max(0) - a.min(0) + 1 in place of a.max(0) + 1.

方法2

这是另一个NumPy基于视图的解决方案,其重点是受 this smart solution by @Eric -

Here's another NumPy's views based solution with focus on performance inspired by this smart solution by @Eric -

def unique_return_inverse_2D_viewbased(a): # a is array
    a = np.ascontiguousarray(a)
    void_dt = np.dtype((np.void, a.dtype.itemsize * np.prod(a.shape[1:])))
    return np.unique(a.view(void_dt).ravel(), return_inverse=1)[1]

样品运行-

In [209]: df
Out[209]: 
    0   1   2   3
0  21   7  31  69
1  62  75  22  62  # ----|
2  16  46   9  31  #     |==> Identical rows, so must have same IDs
3  62  75  22  62  # ----|
4  24  12  88  15

In [210]: unique_return_inverse_2D(df.values)
Out[210]: array([1, 3, 0, 3, 2])

In [211]: unique_return_inverse_2D_viewbased(df.values)
Out[211]: array([1, 3, 0, 3, 2])

这篇关于在pandas.DataFrame的多列上使用numpy.unique的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆