pandas 唯一值多列 [英] pandas unique values multiple columns

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

问题描述

df = pd.DataFrame({'Col1': ['Bob', 'Joe', 'Bill', 'Mary', 'Joe'],'Col2': ['Joe', 'Steve', 'Bob', 'Bob', 'Steve'],'Col3':np.random.random(5)})

返回Col1"和Col2"的唯一值的最佳方法是什么?

所需的输出是

'Bob', 'Joe', 'Bill', 'Mary', 'Steve'

解决方案

pd.unique 从输入数组、DataFrame 列或索引返回唯一值.

此函数的输入需要是一维的,因此需要组合多个列.最简单的方法是选择您想要的列,然后在扁平化的 NumPy 数组中查看值.整个操作如下:

<预><代码>>>>pd.unique(df[['Col1', 'Col2']].values.ravel('K'))数组(['鲍勃','乔','比尔','玛丽','史蒂夫'],dtype=object)

请注意,ravel() 是一个数组方法,它返回多维数组的视图(如果可能).参数 'K' 告诉方法按照元素在内存中的存储顺序来展平数组(pandas 通常将底层数组存储在 Fortran 连续顺序;列在行之前).这比使用该方法的默认C"顺序要快得多.


另一种方法是选择列并将它们传递给 np.unique:

<预><代码>>>>np.unique(df[['Col1', 'Col2']].values)数组(['比尔','鲍勃','乔','玛丽','史蒂夫'],dtype=object)

此处无需使用 ravel(),因为该方法处理多维数组.即便如此,这可能比 pd.unique 慢,因为它使用基于排序的算法而不是哈希表来识别唯一值.

对于较大的 DataFrame,速度差异很显着(尤其是在只有少数唯一值的情况下):

<预><代码>>>>df1 = pd.concat([df]*100000, ignore_index=True) # 500000 行的数据帧>>>%timeit np.unique(df1[['Col1', 'Col2']].values)1 个循环,最好的 3 个:每个循环 1.12 秒>>>%timeit pd.unique(df1[['Col1', 'Col2']].values.ravel('K'))10 个循环,最好的 3 个:每个循环 38.9 毫秒>>>%timeit pd.unique(df1[['Col1', 'Col2']].values.ravel()) # 使用 C 命令进行 ravel10 个循环,最好的 3 个:每个循环 49.9 毫秒

df = pd.DataFrame({'Col1': ['Bob', 'Joe', 'Bill', 'Mary', 'Joe'],
                   'Col2': ['Joe', 'Steve', 'Bob', 'Bob', 'Steve'],
                   'Col3': np.random.random(5)})

What is the best way to return the unique values of 'Col1' and 'Col2'?

The desired output is

'Bob', 'Joe', 'Bill', 'Mary', 'Steve'

解决方案

pd.unique returns the unique values from an input array, or DataFrame column or index.

The input to this function needs to be one-dimensional, so multiple columns will need to be combined. The simplest way is to select the columns you want and then view the values in a flattened NumPy array. The whole operation looks like this:

>>> pd.unique(df[['Col1', 'Col2']].values.ravel('K'))
array(['Bob', 'Joe', 'Bill', 'Mary', 'Steve'], dtype=object)

Note that ravel() is an array method that returns a view (if possible) of a multidimensional array. The argument 'K' tells the method to flatten the array in the order the elements are stored in the memory (pandas typically stores underlying arrays in Fortran-contiguous order; columns before rows). This can be significantly faster than using the method's default 'C' order.


An alternative way is to select the columns and pass them to np.unique:

>>> np.unique(df[['Col1', 'Col2']].values)
array(['Bill', 'Bob', 'Joe', 'Mary', 'Steve'], dtype=object)

There is no need to use ravel() here as the method handles multidimensional arrays. Even so, this is likely to be slower than pd.unique as it uses a sort-based algorithm rather than a hashtable to identify unique values.

The difference in speed is significant for larger DataFrames (especially if there are only a handful of unique values):

>>> df1 = pd.concat([df]*100000, ignore_index=True) # DataFrame with 500000 rows
>>> %timeit np.unique(df1[['Col1', 'Col2']].values)
1 loop, best of 3: 1.12 s per loop

>>> %timeit pd.unique(df1[['Col1', 'Col2']].values.ravel('K'))
10 loops, best of 3: 38.9 ms per loop

>>> %timeit pd.unique(df1[['Col1', 'Col2']].values.ravel()) # ravel using C order
10 loops, best of 3: 49.9 ms per loop

这篇关于 pandas 唯一值多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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