对序列进行处理,包括对序列进行排序、追加一个序列、对序列值进行替换、对序列的值进行更新,并处理序列中出现的缺失值。
按照值或索引对序列进行排序:
Series.sort_values(self, axis=0, ascending=True, inplace=False, kind=‘quicksort‘, na_position=‘last‘) Series.sort_index(self, axis=0, level=None, ascending=True, inplace=False, kind=‘quicksort‘, na_position=‘last‘, sort_remaining=True)
参数注释:
1,按照值来排序
按照序列的值进行排序,Nan放在last位置,
>>> s = pd.Series([np.nan, 1, 3, 10, 5]) >>> s.sort_values() 1 1.0 2 3.0 4 5.0 3 10.0 0 NaN dtype: float64
2,按照索引来排序
按照索引的值进行排序
>>> s = pd.Series([‘a‘, ‘b‘, ‘c‘, ‘d‘], index=[3, 2, 1, 4]) >>> s.sort_index() 1 c 2 b 3 a 4 d dtype: object
使用追加的方法,把序列追加在另一个序列之后,合并为一个新的序列:
Series.append(self, to_append, ignore_index=False, verify_integrity=False)
参数注释:
举个例子,把两个序列合并为一个,当不忽略索引时,把序列的索引作为合并之后的索引;当忽略索引时,新的序列会重建索引。
>>> s1 = pd.Series([1, 2, 3]) >>> s2 = pd.Series([4, 5, 6]) >>> s1.append(s2) 0 1 1 2 2 3 0 4 1 5 2 6 dtype: int64 >>> s1.append(s2, ignore_index=True) 0 1 1 2 2 3 3 4 4 5 5 6 dtype: int64
把序列中的值替换为另一个值:
Series.replace(self, to_replace=None, value=None, inplace=False, limit=None, regex=False, method=‘pad‘)
参数注释:
场景1:参数to_replace是标量,参数value也是标量
>>> s = pd.Series([0, 1, 2, 3, 4]) >>> s.replace(0, 5) 0 5 1 1 2 2 3 3 4 4 dtype: int64
场景2:参数to_replace是列表,参数value是标量,例如,把序列中匹配to_replace列表中的值替换为5
>>> s = pd.Series([0, 1, 2, 3, 4]) >>> s.replace([0,2],5) 0 5 1 1 2 5 3 3 4 4 dtype: int64
场景3,参数to_replace是字典,参数value是None,例如,把序列中匹配字典的key的值替换为字典的value。
>>> s = pd.Series([0, 1, 2, 3, 4]) >>> s.replace({0:5,1:7}) 0 5 1 7 2 2 3 3 4 4 dtype: int64
序列值的更新,有3种方式,第一种方式是使用标量值更新序列的单个值,第二种方式是通过索引切片修改序列的多个值,第三种方式是使用序列来更新一个序列。
1,使用标量来更新序列
索引到索引的单个值,通过赋值来修改序列
>>> s = pd.Series([0, 1, 2, 3, 4]) >>> s.iat[1]=7 >>> s 0 0 1 7 2 2 3 3 4 4 dtype: int64
2,使用切片来更新序列
通过loc属性获得序列的切片,通过赋值一个列表来修改序列的多个值:
>>> s.loc[1:2]=[2,3] >>> s 0 0 1 2 2 3 3 3 4 4 dtype: int64
3,使用序列来更新序列
按照索引对齐方式就地修改序列,也就是说,在修改原始序列的值时,原始序列的索引必须和参数序列进行匹配,把索引相同的值修改为新值。
Series.update(self, other)
举个例子,使用update()函数,修改索引为0和2的值为‘d‘和‘e‘:
>>> s = pd.Series([‘a‘, ‘b‘, ‘c‘]) >>> s.update(pd.Series([‘d‘, ‘e‘], index=[0, 2])) >>> s 0 d 1 b 2 e dtype: object
缺失值使用NumPy.NaN ,NumPy.nan或者None来表示,使用isna()函数来检查是否存在NA,使用dropna()删除序列中的NA值,使用fillna()函数填充缺失值,
1,检查序列是否存在缺失值
>>> s=pd.Series(data=[1,2,np.NaN,4]) >>> s.isna() 0 False 1 False 2 True 3 False dtype: bool
2,删除序列中的缺失值
>>> s.dropna() 0 1.0 1 2.0 3 4.0 dtype: float64
使用fillna()函数,找到缺失数据相邻的有效数据,使用该有效数据来填充缺失值:
Series.fillna(self, value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs)
参数注释:
1,回填
回填是指backfill和bfill 方法,用缺失值之后的第一有效值来填充
>>> s.fillna(method=‘bfill‘) 0 1.0 1 2.0 2 4.0 3 4.0 dtype: float64
2,补填
补填是指 pad和ffill方法,用缺失值之前的有效值来填充
>>> s.fillna(method=‘ffill‘) 0 1.0 1 2.0 2 2.0 3 4.0 dtype: float64
3,固定值填充
当method为None时,使用value参数指定的值来填充缺失值,固定值可以是均值、中位数、和众数,
>>> s.fillna(value=3,method=None) 0 1.0 1 2.0 2 3.0 3 4.0 dtype: float64
使用插补法拟合出缺失的值,然后用拟合值来填充缺失值:
Series.interpolate(self, method=‘linear‘, axis=0, limit=None, inplace=False, limit_direction=‘forward‘, limit_area=None, downcast=None, **kwargs)
参数注释:
常见的插补方法是线性回归和多项式回归
1,线性回归拟合
linear是默认的拟合方法,linear 忽略索引,并发序列值作为等间距,
>>> s.interpolate() 0 1.0 1 2.0 2 3.0 3 4.0 dtype: float64
2,多项式拟合
polynomial表示多项式拟合,需要传递order参数:
>>> s.interpolate(method=‘polynomial‘,order=2) 0 1.0 1 2.0 2 3.0 3 4.0 dtype: float64
参考文档:
pandas 学习 第3篇:序列的处理(排序、连接、替换、更新和缺失值)
原文:https://www.cnblogs.com/ljhdo/p/10279486.html