对序列的处理,
当序列中存在重复值时,可以删除重复值,使序列中的值是唯一的:
Series.drop_duplicates(self, keep=‘first‘, inplace=False)
参数keep:有效值是first(保留第一个,删除后面出现的重复值),last(保留最后一个,删除前面出现的重复值),false(不保留,把重复的数据删除),默认值是保留第一个,
>>> 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来标识:
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)
参数注释:
比如,用take函数获取索引为1和5的序列元素:
>>> s=pd.Series([1,1,2,3,4,4,5]) >>> s.take([1,5]) 1 1 5 4 dtype: int64
检查序列中是否存在特定的值,参数values是集合或列表,从序列中逐个元素比对是否存在values中的值,如果存在,那么该元素所在的位置上设置为True;如果不存在,那么该元素所在的位置上设置为False。
Series.isin(self, values)
该函数返回的是bool序列,例如:
>>> 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, **kwargs)
参数注释:
举个例子:
>>> 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 >>> 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函数
用于对特定的轴进行重命名
Series.rename_axis(self, mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False)
参数注释:
mapper:对轴的name属性进行重命名
index,columns:scalar, list-like, dict-like or function
axis:{0 or ‘index’, 1 or ‘columns’}, default 0,对轴重命名
从序列中随机取样,取样的数量由参数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
参考文档:
pandas 学习 第5篇:序列的处理(重复值、重新索引、选择、截断、取样)
原文:https://www.cnblogs.com/ljhdo/p/10570358.html