對序列進行數據數據,正則表達式
當序列中存在重複值時,能夠刪除重複值,使序列中的值是惟一的:數組
Series.drop_duplicates(self, keep='first', inplace=False)
參數keep:有效值是first(保留第一個,刪除後面出現的重複值),last(保留最後一個,刪除前面出現的重複值),false(不保留,把重複的數據刪除),默認值是保留第一個,app
>>> s=pd.Series([1,1,2,3,4,4,5]) >>> s.drop_duplicates() 0 1 2 2 3 3 4 4 6 5 dtype: int64
把序列中出現重複值的位置用True來標識:dom
Series.duplicated(self, keep='first')
從序列中選擇前n行、後n行、任意連續位置的數據函數
Series.head(self, n=5) Series.tail(self, n=5) Series.take(self, indices, axis=0, is_copy=False, **kwargs)
參數註釋:spa
好比,用take函數獲取索引爲1和5的序列元素:scala
>>> s=pd.Series([1,1,2,3,4,4,5]) >>> s.take([1,5]) 1 1 5 4 dtype: int64
檢查序列中是否存在特定的值,參數values是集合或列表,從序列中逐個元素比對是否存在values中的值,若是存在,那麼該元素所在的位置上設置爲True;若是不存在,那麼該元素所在的位置上設置爲False。rest
Series.isin(self, values)
該函數返回的是bool序列,例如:code
>>> s = pd.Series(['lama', 'cow', 'lama', 'beetle', 'lama', ... 'hippo'], name='animal') >>> s.isin(['cow', 'lama']) 0 True 1 True 2 True 3 False 4 True 5 False Name: animal, dtype: bool
把序列中,按照索引來截斷,參數before表示在索引前,after表示在索引後,截斷before以前和after以後的元素,保留before和after之間的序列元素,注意,包含before和after所在的索引:對象
Series.truncate(self, before=None, after=None, axis=None, copy=True)
該函數的做用相似於切片:
>>> s.truncate(before=2,after=5) 2 2 3 3 4 4 5 4 dtype: int64
pandas提供兩個函數,where函數用於把條件爲False的元素替換爲指定值,mask函數用於把條件爲True的元素替換爲指定值:
Series.where(self, cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False) Series.mask(self, cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False)
參數註釋:
舉個例子,把序列中大於1的元素替換爲7:
>>> s=pd.Series(range(5)) >>> s.mask(s>1,7) 0 0 1 1 2 7 3 7 4 7 dtype: int64 >>> s.where(s<=1,7) 0 0 1 1 2 7 3 7 4 7 dtype: int64
根據索引來截取子集:
Series.filter(self, items=None, like=None, regex=None, axis=None)
參數註釋:
使原始序列和新的索引保持一致,原始索引和新索引是按照索引對齊的方式來匹配的,在重索引的過兩次,可使用可選的填充邏輯,把不存在於原始索引中的值設置爲NA。
Series.reindex(self, index=None, **kwargs)
參數註釋:
重索引的目的是使原始索引按照新的索引進行排序
>>> s=pd.Series(range(5)) >>> s.reindex(index=[4,3,2,1,0]) 4 4 3 3 2 2 1 1 0 0 dtype: int64
reset_index函數重置索引,並建立一個新的序列,該函數適用於須要把索引做爲一列的狀況,或者須要把索引重置成默認值。
Series.reset_index(self, level=None, drop=False, name=None, inplace=False)
參數註釋:
舉個例子,對序列重置索引,生成一個數據框:
>>> s = pd.Series([1, 2, 3, 4], name='foo', ... index=pd.Index(['a', 'b', 'c', 'd'], name='idx')) >>> s.reset_index() idx foo 0 a 1 1 b 2 2 c 3 3 d 4
重命名序列的name屬性或索引標籤
1,rename函數用於重名軸標籤
對於rename函數,若是參數是單個字符串,那麼修改的是序列的name屬性;若是是函數或list-like結構,那麼重命名的是索引標籤:
Series.rename(self, index=None,copy=True, inplace=False, level=None, **kwargs)
參數註釋:
index:標量,dick-like 或 函數,若是index參數是dick-like或函數,用於修改序列的行索引標籤,若是index參數是字符串標量,用於修改序列的name屬性
舉個例子,使用rename()來修改序列的name屬性,
>>> s = pd.Series([1, 2, 3]) >>> s 0 1 1 2 2 3 dtype: int64 >>> s.rename("my_name") # scalar, changes Series.name 0 1 1 2 2 3 Name: my_name, dtype: int64
使用rename()函數來修改序列的行索引:
>>> s.rename(lambda x: x ** 2) # function, changes labels 0 1 1 2 4 3 dtype: int64 >>> s.rename({1: 3, 2: 5}) # mapping, changes labels 0 1 3 2 5 3 dtype: int64
2,rename_axis函數用於重命名軸的name屬性
用於對特定的軸進行重命名
Series.rename_axis(self, mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False)
參數註釋:
從序列中隨機取樣,取樣的數量由參數n,或者frac來決定:
Series.sample(self, n=None, frac=None, replace=False, weights=None, random_state=None, axis=None)
參數註釋:
例如,從序列隨機取樣2個:
>>> s=pd.Series(range(5),name='abc') >>> s.sample(n=2) 2 2 4 4 Name: abc, dtype: int64
參考文檔: