Python 在 Pandas 数据框中查找唯一值,而不管行或列位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20084382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find unique values in a Pandas dataframe, irrespective of row or column location
提问by user1717931
I have a Pandas dataframe and I want to find all the unique values in that dataframe...irrespective of row/columns. If I have a 10 x 10 dataframe, and suppose they have 84 unique values, I need to find them - Not the count.
我有一个 Pandas 数据框,我想在该数据框中找到所有唯一值......无论行/列如何。如果我有一个 10 x 10 的数据框,并假设它们有 84 个唯一值,我需要找到它们 - 而不是计数。
I can create a set and add the values of each rows by iterating over the rows of the dataframe. But, I feel that it may be inefficient (cannot justify that). Is there an efficient way to find it? Is there a predefined function?
我可以创建一个集合并通过遍历数据帧的行来添加每行的值。但是,我觉得它可能效率低下(不能证明这一点)。有没有一种有效的方法可以找到它?有预定义的功能吗?
采纳答案by Jeff
In [1]: df = DataFrame(np.random.randint(0,10,size=100).reshape(10,10))
In [2]: df
Out[2]:
0 1 2 3 4 5 6 7 8 9
0 2 2 3 2 6 1 9 9 3 3
1 1 2 5 8 5 2 5 0 6 3
2 0 7 0 7 5 5 9 1 0 3
3 5 3 2 3 7 6 8 3 8 4
4 8 0 2 2 3 9 7 1 2 7
5 3 2 8 5 6 4 3 7 0 8
6 4 2 6 5 3 3 4 5 3 2
7 7 6 0 6 6 7 1 7 5 1
8 7 4 3 1 0 6 9 7 7 3
9 5 3 4 5 2 0 8 6 4 7
In [13]: Series(df.values.ravel()).unique()
Out[13]: array([9, 1, 4, 6, 0, 7, 5, 8, 3, 2])
Numpy unique sorts, so its faster to do it this way (and then sort if you need to)
Numpy 独特的排序,所以这样做会更快(如果需要,然后排序)
In [14]: df = DataFrame(np.random.randint(0,10,size=10000).reshape(100,100))
In [15]: %timeit Series(df.values.ravel()).unique()
10000 loops, best of 3: 137 ?s per loop
In [16]: %timeit np.unique(df.values.ravel())
1000 loops, best of 3: 270 ?s per loop
回答by user1506145
Or you can use:
或者你可以使用:
df.stack().unique()
df.stack().unique()
Then you don't need to worry if you have NaNvalues, as they are excluded when doing the stacking.
那么您不必担心是否有NaN值,因为在进行堆叠时它们被排除在外。

