針對Series對象,從中抽取信息
unique可以得到Series對象的唯一值數組
>>> obj = Series(['c','a','d','a','a','b','b','c','c']) >>> obj.unique() array(['c', 'a', 'd', 'b'], dtype=object) >>> obj 0 c 1 a 2 d 3 a 4 a 5 b 6 b 7 c 8 c dtype: object >>> type(obj.unique()) <class 'numpy.ndarray'>#注意這里返回的不再是Series對象,而是ndarray的一維數組
返回的是未排序的數組,如果需要排序,再次執行sort()方法或者用numpy的頂級函數sort()
>>> new_array = obj.unique() >>> new_array array(['c', 'a', 'd', 'b'], dtype=object) >>> new_array.sort() >>> new_array array(['a', 'b', 'c', 'd'], dtype=object) >>> import numpy as np >>> new_array = obj.unique() >>> new_array array(['c', 'a', 'd', 'b'], dtype=object) >>> na = np.sort(new_array) >>> na array(['a', 'b', 'c', 'd'], dtype=object)
值計數
用到value_counts方法或value_count頂級函數
>>> obj 0 c 1 a 2 d 3 a 4 a 5 b 6 b 7 c 8 c dtype: object >>> obj_c= obj.value_counts() >>> obj_c c 3 a 3 b 2 d 1 dtype: int64 >>> pd.value_counts(obj)#默認是降序 c 3 a 3 b 2 d 1 dtype: int64 >>> pd.value_counts(obj,sort =False)#對統計結果不排序 a 3 b 2 d 1 c 3 dtype: int64
isin用於判斷矢量化集合的成員資格,可以用於選取Series或DataFrame列中的數據子集
>>> mask = obj.isin(['a','c']) >>> mask 0 True 1 True 2 False 3 True 4 True 5 False 6 False 7 True 8 True dtype: bool >>> obj[mask] 0 c 1 a 3 a 4 a 7 c 8 c dtype: object
可以將value_counts的頂級函數傳給DataFrame對象的apply()使用,以便統計一列或者一行的值的個數