Python 查找列中的唯一值,然后对它们进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32072076/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:57:35  来源:igfitidea点击:

Find the unique values in a column and then sort them

pythonpandassortingdataframeunique

提问by MAS

I have a pandas dataframe. I want to print the unique values of one of its columns in ascending order. This is how I am doing it:

我有一个熊猫数据框。我想按升序打印其中一列的唯一值。这就是我的做法:

import pandas as pd
df = pd.DataFrame({'A':[1,1,3,2,6,2,8]})
a = df['A'].unique()
print a.sort()

The problem is that I am getting a Nonefor the output.

问题是我得到了一个None输出。

采纳答案by Vineet Kumar Doshi

sortedreturn a new sorted list from the items in iterable.

CODE

sorted从 iterable 中的项目返回一个新的排序列表。

代码

import pandas as pd
df = pd.DataFrame({'A':[1,1,3,2,6,2,8]})
a = df['A'].unique()
print sorted(a)

OUTPUT

输出

[1, 2, 3, 6, 8]

回答by EdChum

sortsorts inplace so returns nothing:

sort就地排序,因此不返回任何内容:

In [54]:
df = pd.DataFrame({'A':[1,1,3,2,6,2,8]})
a = df['A'].unique()
a.sort()
a

Out[54]:
array([1, 2, 3, 6, 8], dtype=int64)

So you have to call print aagain after the call to sort.

因此,您必须在调用print a之后再次调用sort

Eg.:

例如。:

In [55]:
df = pd.DataFrame({'A':[1,1,3,2,6,2,8]})
a = df['A'].unique()
a.sort()
print(a)

[1 2 3 6 8]

回答by Challensois

I would suggest using numpy's sort, as it is anyway what pandas is doing in background:

我建议使用 numpy 的排序,因为无论如何熊猫在后台做什么:

import numpy as np
np.sort(df.A.unique())

But doing all in pandas is valid as well.

但是在 Pandas 中做所有事情也是有效的。

回答by Meloun

You can also use the drop_duplicates()instead of unique()

您还可以使用drop_duplicates()而不是 unique()

df = pd.DataFrame({'A':[1,1,3,2,6,2,8]})
a = df['A'].drop_duplicates()
a.sort()
print a

回答by MDMoore313

I prefer the oneliner:

我更喜欢oneliner:

print(sorted(df['Column Name'].unique()))

回答by Bowen Liu

Came across the question myself today. I think the reason that your code returns 'None' (exactly what I got by using the same method) is that

今天自己遇到了这个问题。我认为您的代码返回“无”的原因(正是我使用相同方法得到的)是

a.sort()

is calling the sort function to mutate the list a. In my understanding, this is a modification command. To see the result you have to use print(a).

正在调用排序函数来改变列表 a。在我的理解中,这是一个修改命令。要查看结果,您必须使用 print(a)。

My solution, as I tried to keep everything in pandas:

我的解决方案,因为我试图将所有内容都保存在熊猫中:

pd.Series(df['A'].unique()).sort_values()

回答by Ivan Carrasco Quiroz

Another way is using setdata type.

另一种方法是使用set数据类型。

Some characteristic of Sets:Sets are unordered, can include mixed data types, elements in a set cannot be repeated, are mutable.

集合的一些特性:集合是无序的,可以包含混合数据类型,集合中的元素不能重复,是可变的。

Solving your question:

解决您的问题:

df = pd.DataFrame({'A':[1,1,3,2,6,2,8]})
sorted(set(df.A))

The answer in Listtype:

列表类型的答案:

[1, 2, 3, 6, 8]