pandas 如何获取特定列的值的所有唯一组合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41312590/
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
How to obtain all unique combinations of values of particular columns
提问by Dinosaurius
I want to extract all unique combinations of values of columns Col1
, Col2
and Col3
. Let's say there is the following dataframe df
:
我想提取列值的所有唯一组合Col1
,Col2
和Col3
。假设有以下数据框df
:
df =
Col1 Col2 Col3
12 AB 13
11 AB 13
12 AB 13
12 AC 14
The answer is:
答案是:
unique =
Col1 Col2 Col3
12 AB 13
11 AB 13
12 AC 14
I know how to obtain unique values of a particular column, i.e. df.Col1.unique()
, however not sure about unique combinations.
我知道如何获取特定列的唯一值,即df.Col1.unique()
,但不确定唯一组合。
回答by Roman Pekar
There is a method for this - pandas.DataFrame.drop_duplicates
:
有一个方法 - pandas.DataFrame.drop_duplicates
:
>>> df.drop_duplicates()
Col1 Col2 Col3
0 12 AB 13
1 11 AB 13
3 12 AC 14
You can do it inplace
as well:
你也可以这样做inplace
:
>>> df.drop_duplicates(inplace=True)
>>> df
Col1 Col2 Col3
0 12 AB 13
1 11 AB 13
3 12 AC 14
If you need to get unique values of certain columns:
如果您需要获取某些列的唯一值:
>>> df[['Col2','Col3']].drop_duplicates()
Col2 Col3
0 AB 13
3 AC 14
as @jezrael suggests, you can also consider using subset
parameter of drop_duplicates()
:
正如@jezrael 所建议的,您还可以考虑使用以下subset
参数drop_duplicates()
:
>>> df.drop_duplicates(subset=['Col2','Col3'])
Col1 Col2 Col3
0 12 AB 13
3 12 AC 14