Pandas-Series接口第五讲 缺失值处理

268 阅读3分钟

Series第五讲 缺失值处理

本节课将讲解如何处理pandas里的缺失值

缺失值处理

  • Series.fillnan()
  • Series.backfill()
  • Series.bfill()
  • Series.pad()
  • Series.ffill()
  • Series.dropnan()
  • Series.interpolate()
  • Series.isna()
  • Series.isnull()
  • Series.notna()
  • Series.notnall()
  • Series.replace()

详细介绍

先来创建一个Series

In [4]: s = pd.Series([1, 2, 3, None, 5, None, None, None, 9])                  

In [5]: s                                                                       
Out[5]: 
0    1.0
1    2.0
2    3.0
3    NaN
4    5.0
5    NaN
6    NaN
7    NaN
8    9.0
dtype: float64

1. Series.fillna()

Series.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None)

用特殊的method填充缺失值

常用参数介绍:
  • values:calar, dict, Series, or DataFrame 【填充nan的值,不能是list】。
  • method:{‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None}, default None 【填充方法,用前/后面的值进行填充】
  • axis:{0 or ‘index’} 【前后或上下填充】
  • limit:int, default None 【连续填充的值的数量】
In [6]: s.fillna(0)                                                             
Out[6]: 
0    1.0
1    2.0
2    3.0
3    0.0
4    5.0
5    0.0
6    0.0
7    0.0
8    9.0
dtype: float64

In [7]: s.fillna(0, limit=2)                                                    
Out[7]: 
0    1.0
1    2.0
2    3.0
3    0.0
4    5.0
5    0.0
6    NaN
7    NaN
8    9.0
dtype: float64

# 只对nan值进行填充
In [8]: s.fillna({0: 'A', 3: 'A'})                                              
Out[8]: 
0      1
1      2
2      3
3      A
4      5
5    NaN
6    NaN
7    NaN
8      9
dtype: object

# 用nan后面的值进行填充
In [11]: s.fillna(method='bfill')                                               
Out[11]: 
0    1.0
1    2.0
2    3.0
3    5.0
4    5.0
5    9.0
6    9.0
7    9.0
8    9.0
dtype: float64

2. Series.backfill()

Series.backfill(axis=None, inplace=False, limit=None, downcast=None)

等于 method='bfill' 的 fillna 方法

3. Series.bfill()

等于 method='bfill' 的 fillna 方法

4. Series.pad()

Series.pad(axis=None, inplace=False, limit=None, downcast=None)

等于 method='ffill' 的 fillna 方法

5. Series.ffill()

Series.ffill(axis=None, inplace=False, limit=None, downcast=None)

等于 method='ffill' 的 fillna 方法

6. Series.dropna()

Series.dropna(axis=0, inplace=False, how=None)

删除缺失值

In [13]: s.dropna()                                                             
Out[13]: 
0    1.0
1    2.0
2    3.0
4    5.0
8    9.0
dtype: float64

7. Series.interpolate()

Series.interpolate(method='linear', axis=0, limit=None, inplace=False, limit_direction=None, limit_area=None, downcast=None, **kwargs)

进行插值,默认线性方法

In [15]: s.interpolate()                                                        
Out[15]: 
0    1.0
1    2.0
2    3.0
3    4.0
4    5.0
5    6.0
6    7.0
7    8.0
8    9.0
dtype: float64

8. Series.isna()

Series.isna()

检测缺失值,缺失值为True,非缺失值为False

In [16]: s.isna()                                                               
Out[16]: 
0    False
1    False
2    False
3     True
4    False
5     True
6     True
7     True
8    False
dtype: bool

9. Series.isnull()

Series.isnull()

检测缺失值,同 isna()

10. Series.notna()

Series.notna()

检测非缺失值,和 isna() 方法相反,非缺失值为True,缺失值为False

In [17]: s.notna()                                                              
Out[17]: 
0     True
1     True
2     True
3    False
4     True
5    False
6    False
7    False
8     True
dtype: bool

11. Series.notnull()

Series.notnull()

同 notna()

12. Series.replace()

Series.replace(to_replace=None, value=None, inplace=False, limit=None, regex=False, method='pad')

将 to_replace 匹配到的值替换成 value 参数值

常用参数介绍:
  • to_replace:str, regex, list, dict, Series, int, float, or None 【如何找到将被替换的值】。
  • values:calar, dict, list, str, regex, default None
  • regex:bool or same types as to_replace, default False 【是否将to_replace和或value解释为正则表达式。如果为True则to_replace必须为字符串。或者,这可以是正则表达式或正则表达式的列表,字典或数组,在这种情况下 to_replace必须为None】
  • method:{‘pad’, ‘ffill’, ‘bfill’, None} 【当 to_replace 是 a scalar, list or tuple 且 value 为 None】
# 用A替换值1
In [20]: s.replace(1, 'A')                                                      
Out[20]: 
0      A
1      2
2      3
3    NaN
4      5
5    NaN
6    NaN
7    NaN
8      9
dtype: object

# 值1和5用A替换
In [22]: s.replace({1: 'A', 5: 'A'})                                            
Out[22]: 
0      A
1      2
2      3
3    NaN
4      A
5    NaN
6    NaN
7    NaN
8      9
dtype: object

# 值1,2,3用A替换
In [23]: s.replace([1, 2, 3], 'A')                                              
Out[23]: 
0      A
1      A
2      A
3    NaN
4      5
5    NaN
6    NaN
7    NaN
8      9
dtype: object

# 正则
In [49]: s.apply(str).replace(r'\d', 'A', regex=True)                           
Out[49]: 
0    A.A
1    A.A
2    A.A
3    nan
4    A.A
5    nan
6    nan
7    nan
8    A.A
dtype: object

周末也要继续 坚持 ✊ ✊ ✊!!!