Pandas中Series對象的唯一值
unique()函數用於獲取Series對象的唯一值。
唯一性按出現順序返回。基於哈希表的唯一,因此不排序
以NumPy數組形式返回唯一值。如果是擴展數組支持的Series,則返回僅具有唯一值的該類型的新ExtensionArray
The unique() function is used to get unique values of Series object.
Uniques are returned in order of appearance. Hash table-based unique, therefore does NOT sort.
Syntax:
Series.unique(self)
Returns: ndarray or ExtensionArray
The unique values returned as a NumPy array. See Notes.
Notes: Returns the unique values as a NumPy array. In case of an extension-array backed Series, a new ExtensionArray of that type with just the unique values is returned. This includes
- Categorical
- Period
- Datetime with Timezone
- Interval
- Sparse
- IntegerNA
Examples
import numpy as np import pandas as pd
pd.Series([2, 4, 3, 3], name='P').unique()
pd.Series([pd.Timestamp('2019-01-01') for _ in range(3)]).unique()
pd.Series([pd.Timestamp('2019-01-01', tz='US/Eastern') for _ in range(3)]).unique()
An unordered Categorical will return categories in the order of appearance.
pd.Series(pd.Categorical(list('qppqr'))).unique()
An ordered Categorical preserves the category ordering.
pd.Series(pd.Categorical(list('qppqr'), categories=list('pqr'), ordered=True)).unique()