pandas.core.groupby.SeriesGroupBy.unique#

SeriesGroupBy.unique()[源代码][源代码]#

为每个组返回唯一值。

它为每个分组的值返回唯一值。按出现顺序返回。基于哈希表的唯一性,因此不排序。

返回:
系列

每个分组值的唯一值。

参见

Series.unique

返回 Series 对象的唯一值。

例子

>>> df = pd.DataFrame(
...     [
...         ("Chihuahua", "dog", 6.1),
...         ("Beagle", "dog", 15.2),
...         ("Chihuahua", "dog", 6.9),
...         ("Persian", "cat", 9.2),
...         ("Chihuahua", "dog", 7),
...         ("Persian", "cat", 8.8),
...     ],
...     columns=["breed", "animal", "height_in"],
... )
>>> df
       breed     animal   height_in
0  Chihuahua        dog         6.1
1     Beagle        dog        15.2
2  Chihuahua        dog         6.9
3    Persian        cat         9.2
4  Chihuahua        dog         7.0
5    Persian        cat         8.8
>>> ser = df.groupby("animal")["breed"].unique()
>>> ser
animal
cat              [Persian]
dog    [Chihuahua, Beagle]
Name: breed, dtype: object