pandas 学习 第5篇:序列的处理(重复值、重新索引、选择、截断、取样)
作者:互联网
对序列的处理,
一,删除序列中的重复值
当序列中存在重复值时,可以删除重复值,使序列中的值是唯一的:
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)
参数注释:
- indices:一维数组,用于表示axis的序号,如果axis=0,那么indices表示的是索引
- axis:0表示索引,1表示列(columns),对于序列,axis的值只能是0.
比如,用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)
参数注释:
- cond:逻辑表达式
- other:替换的值
- errors:有效值是‘raise’, ‘ignore’,默认值是raise表示允许异常抛出
- try_cast:bool类型,表示尽可能把返回的结果转换为输入类型
举个例子,把序列中大于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)
参数注释:
- items:保留的标签列表,如果axis是index,那么items代表行标签;如果axis是colums,那么items代表列标签(列名)
- like:用字符串来表示保留的标签
- regex:用正则表达式来表示保留的标签
- axis:过滤的轴,axis可以是数字或字符串,0或index代表行索引,1或columns代表列名称
八,重索引
使原始序列和新的索引保持一致,原始索引和新索引是按照索引对齐的方式来匹配的,在重索引的过两次,可以使用可选的填充逻辑,把不存在于原始索引中的值设置为NA。
Series.reindex(self, index=None, **kwargs)
参数注释:
- index:新索引,必需参数
- method:有效值是None, ‘backfill’/’bfill’, ‘pad’/’ffill’, ‘nearest’,
- None表示不会填充
- ‘backfill’/’bfill’:表示回填,用NA的后面第一个有效值来填充当前的NA
- ‘pad’/’ffill’:表示补填,用前面第一个有效值来填充当前的NA
- ‘nearest’:用最接近NA的有效值来填充当前的NA
- copy:默认值是True,返回新的对象
- fill_value:标量值,默认值是np.NaN,用于对缺失值进行填充的值
- limit:填充的最大次数
- tolerance:可选参数,表示不能完全匹配的原始标签和新标签之间的最大距离,匹配位置处的索引值满足:abs(index_position - target_position)<= tolerance,容差可以是标量值(对所有序列值应用相同的容差),也可以是list-like结构(对每个序列元素应用可变容差),list-like结构包括列表、元组、数组和序列,并且list-like结构的长度和序列的长度和长度必须相同。
重索引的目的是使原始索引按照新的索引进行排序
>>> 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)
参数注释:
- level:对于一个拥有多级索引的序列来说,通过level来指定需要移除的索引,默认值None,移除所有的索引。
- drop:bool值,默认值是False,表示不删除索引,而是把索引作为新的一列来显示,返回值是数据框。如果设置为True,表示把原始索引删除,重置一个新的索引,返回值是序列。
- name:用于对包含序列值的那一列进行命名,默认值是序列的name属性
- inplace:是否就地修改序列
举个例子,对序列重置索引,生成一个数据框:
>>> 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)
参数注释:
- index:dick-like 或 函数 用于修改序列的索引,字符串标量用于修改序列的name属性
- copy:
- inplace:
- level:
举个例子:
>>> 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)
参数注释:
- n:取样的数量
- frac:取样的比例
- weights:概率权重,
- random_state:如果值是整数,表示随机数产生器的种子(seed);如果是numpy.random.RandomState,那么使用该对象来产生随机数。
- axis:轴
例如,从序列随机取样2个:
>>> s=pd.Series(range(5),name='abc') >>> s.sample(n=2) 2 2 4 4 Name: abc, dtype: int64
参考文档:
标签:None,False,Series,索引,取样,序列,pandas,axis 来源: https://www.cnblogs.com/ljhdo/p/10570358.html