pandas7-缺失数据

第七章 缺失数据

import numpy as np
import pandas as pd

一、缺失值的统计和删除

缺失信息的统计

缺失数据可以使用 isna 或 isnull (两个函数没有区别)来查看每个单元格是否缺失,通过和 sum 的组合
可以计算出每列缺失值的比例:

df = pd.read_csv('data/learn_pandas.csv',usecols = ['Grade', 'Name', 'Gender', 'Height','Weight', 'Transfer'])
df.isna().head()
Grade Name Gender Height Weight Transfer
0 False False False False False False
1 False False False False False False
2 False False False False False False
3 False False False True False False
4 False False False False False False
df.isna().sum()/df.shape[0] # 查看缺失的比例
Grade       0.000
Name        0.000
Gender      0.000
Height      0.085
Weight      0.055
Transfer    0.060
dtype: float64

如果想要查看某一列缺失或者非缺失的行,可以利用 Series 上的 isna 或者 notna 进行布尔索引。例如,
查看身高缺失的行:

df[df.Height.isna()].head()
Grade Name Gender Height Weight Transfer
3 Sophomore Xiaojuan Sun Female NaN 41.0 N
12 Senior Peng You Female NaN 48.0 NaN
26 Junior Yanli You Female NaN 48.0 N
36 Freshman Xiaojuan Qin Male NaN 79.0 Y
60 Freshman Yanpeng Lv Male NaN 65.0 N

如果想要同时对几个列,检索出全部为缺失或者至少有一个缺失或者没有缺失的行,可以使用 isna,
notna 和 any, all 的组合。例如,对身高、体重和转系情况这3列分别进行这三种情况的检索:

sub_set = df[['Height', 'Weight', 'Transfer']]
df[sub_set.isna().all(1)] # 全部缺失
Grade Name Gender Height Weight Transfer
102 Junior Chengli Zhao Male NaN NaN NaN
df[sub_set.isna().any(1)].head() # 至少有一个缺失
Grade Name Gender Height Weight Transfer
3 Sophomore Xiaojuan Sun Female NaN 41.0 N
9 Junior Juan Xu Female 164.8 NaN N
12 Senior Peng You Female NaN 48.0 NaN
21 Senior Xiaopeng Shen Male 166.0 62.0 NaN
26 Junior Yanli You Female NaN 48.0 N
df[sub_set.notna().all(1)].head() # 没有缺失
Grade Name Gender Height Weight Transfer
0 Freshman Gaopeng Yang Female 158.9 46.0 N
1 Freshman Changqiang You Male 166.5 70.0 N
2 Senior Mei Sun Male 188.9 89.0 N
4 Sophomore Gaojuan You Male 174.0 74.0 N
5 Freshman Xiaoli Qian Female 158.0 51.0 N

2. 缺失信息的删除

数据处理中经常需要根据缺失值的大小、比例或其他特征来进行行样本或列特征的删除, pandas 中提供
了 dropna 函数来进行操作。
dropna 的主要参数为轴方向 axis (默认为0,即删除行)、删除方式 how 、删除的非缺失值个数阈值
thresh ( 非缺失值 没有达到这个数量的相应维度会被删除)、备选的删除子集 subset ,其中 how 主要
有 any 和 all 两种参数可以选择。
例如,删除身高体重至少有一个缺失的行:

res = df.dropna(how = 'any', subset = ['Height', 'Weight'])
res.shape
(174, 6)
# 例如,删除超过15个缺失值的列:
res = df.dropna(1, thresh=df.shape[0]-15) # 身高被删除
res.head()
Grade Name Gender Weight Transfer
0 Freshman Gaopeng Yang Female 46.0 N
1 Freshman Changqiang You Male 70.0 N
2 Senior Mei Sun Male 89.0 N
3 Sophomore Xiaojuan Sun Female 41.0 N
4 Sophomore Gaojuan You Male 74.0 N
#当然,不用 dropna 同样是可行的,例如上述的两个操作,也可以使用布尔索引来完成:
res = df.loc[df[['Height', 'Weight']].notna().all(1)]
res.shape
(174, 6)
res = df.loc[:, ~(df.isna().sum()>15)]
res.head()
Grade Name Gender Weight Transfer
0 Freshman Gaopeng Yang Female 46.0 N
1 Freshman Changqiang You Male 70.0 N
2 Senior Mei Sun Male 89.0 N
3 Sophomore Xiaojuan Sun Female 41.0 N
4 Sophomore Gaojuan You Male 74.0 N

二、缺失值的填充和插值

1. 利用fillna进行填充

在 fillna 中有三个参数是常用的: value, method, limit 。其中, value 为填充值,可以是标量,也可
以是索引到元素的字典映射; method 为填充方法,有用前面的元素填充 ffill 和用后面的元素填充
bfill 两种类型, limit 参数表示连续缺失值的最大填充次数。

# 下面构造一个简单的 Series 来说明用法:
s = pd.Series([np.nan, 1, np.nan, np.nan, 2, np.nan],list('aaabcd'))
s
a    NaN
a    1.0
a    NaN
b    NaN
c    2.0
d    NaN
dtype: float64
s.fillna(method='ffill') # 用前面的值向后填充
a    NaN
a    1.0
a    1.0
b    1.0
c    2.0
d    2.0
dtype: float64
s.fillna(method='ffill', limit=1) # 连续出现的缺失,最多填充一次
a    NaN
a    1.0
a    1.0
b    NaN
c    2.0
d    2.0
dtype: float64
s.fillna(s.mean()) # value为标量
a    1.5
a    1.0
a    1.5
b    1.5
c    2.0
d    1.5
dtype: float64
s.fillna({
    
    'a': 100, 'd': 200}) # 通过索引映射填充的值
a    100.0
a      1.0
a    100.0
b      NaN
c      2.0
d    200.0
dtype: float64
# 有时为了更加合理地填充,需要先进行分组后再操作。例如,根据年级进行身高的均值填充:
df.groupby('Grade')['Height'].transform(lambda x: x.fillna(x.mean())).head()
0    158.900000
1    166.500000
2    188.900000
3    163.075862
4    174.000000
Name: Height, dtype: float64

2. 插值函数

在关于 interpolate 函数的 文档 描述中,列举了许多插值法,包括了大量 Scipy 中的方法。由于很多插
值方法涉及到比较复杂的数学知识,因此这里只讨论比较常用且简单的三类情况,即线性插值、最近邻
插值和索引插值。
对于 interpolate 而言,除了插值方法(默认为 linear 线性插值)之外,有与 fillna 类似的两个常用参
数,一个是控制方向的 limit_direction ,另一个是控制最大连续缺失值插值个数的 limit 。其中,限制
插值的方向默认为 forward ,这与 fillna 的 method 中的 ffill 是类似的,若想要后向限制插值或者双向
限制插值可以指定为 backward 或 both 。

s = pd.Series([np.nan, np.nan, 1,np.nan, np.nan, np.nan,2, np.nan, np.nan])
s.values
array([nan, nan,  1., nan, nan, nan,  2., nan, nan])
# 例如,在默认线性插值法下分别进行 backward 和双向限制插值,同时限制最大连续条数为1:
res = s.interpolate(limit_direction='backward', limit=1)
res.values

array([ nan, 1.  , 1.  ,  nan,  nan, 1.75, 2.  ,  nan,  nan])
res = s.interpolate(limit_direction='both', limit=1)
res.values
array([ nan, 1.  , 1.  , 1.25,  nan, 1.75, 2.  , 2.  ,  nan])
# 第二种常见的插值是最近邻插补,即缺失值的元素和离它最近的非缺失值元素一样:
s.interpolate('nearest').values
array([nan, nan,  1.,  1.,  1.,  2.,  2., nan, nan])
# 最后来介绍索引插值,即根据索引大小进行线性插值。例如,构造不等间距的索引进行演示:
s = pd.Series([0,np.nan,10],index=[0,1,10])
s
0      0.0
1      NaN
10    10.0
dtype: float64
s.interpolate() # 默认的线性插值,等价于计算中点的值
0      0.0
1      5.0
10    10.0
dtype: float64
s.interpolate(method='index') # 和索引有关的线性插值,计算相应索引大小对应的值
0      0.0
1      1.0
10    10.0
dtype: float64
# 同时,这种方法对于时间戳索引也是可以使用的,有关时间序列的其他话题会在第十章进行讨论,这里举一个简单的例子:
s = pd.Series([0,np.nan,10],index=pd.to_datetime(['20200101','20200102','20200111']))
s
2020-01-01     0.0
2020-01-02     NaN
2020-01-11    10.0
dtype: float64
s.interpolate()
2020-01-01     0.0
2020-01-02     5.0
2020-01-11    10.0
dtype: float64

三、Nullable类型

1. 缺失记号及其缺陷

在 python 中的缺失值用 None 表示,该元素除了等于自己本身之外,与其他任何元素不相等

None == None
True
None == False
False
None == []
False
None == ''
False

在 numpy 中利用 np.nan 来表示缺失值,该元素除了不和其他任何元素相等之外,和自身的比较结果也返
回 False :

np.nan == np.nan
False
np.nan == None
False
np.nan == False
False

在 pandas 中无论是 None 还是 np.nan ,存入序列或表中都会以 np.nan 的形式存储:

pd.Series([1, None])
0    1.0
1    NaN
dtype: float64
pd.Series([1, np.nan])
0    1.0
1    NaN
dtype: float64
s1 = pd.Series([1, np.nan])
s1 == 1
0     True
1    False
dtype: bool
s2 = pd.Series([1, 2])
s3 = pd.Series([1, np.nan])
s1.equals(s2)
False
s1.equals(s3)
True

在时间序列的对象中, pandas 利用 pd.NaT 来指代缺失值,它的作用和 np.nan 是一致的(时间序列的对
象和构造将在第十章讨论)

pd.to_timedelta(['30s', np.nan]) # Timedelta中的NaT
TimedeltaIndex(['0 days 00:00:30', NaT], dtype='timedelta64[ns]', freq=None)
pd.to_datetime(['20200101', np.nan]) # Datetime中的NaT
DatetimeIndex(['2020-01-01', 'NaT'], dtype='datetime64[ns]', freq=None)

那么为什么要引入 pd.NaT 来表示时间对象中的缺失呢?仍然以 np.nan 的形式存放会有什么问题?在
pandas 中可以看到 object 类型的对象,而 object 是一种混杂对象类型,如果出现了多个类型的元素同
时存储在 Series 中,它的类型就会变成 object 。例如,同时存放整数和字符串的列表:

pd.Series([1, 'two'])
0      1
1    two
dtype: object

NaT 问题的根源来自于 np.nan 的本身是一种浮点类型,而如果浮点和时间类型混合存储,如果不设计新
的内置缺失类型来处理,就会变成含糊不清的 object 类型,这显然是不希望看到的。

type(np.nan)
float

同时,由于 np.nan 的浮点性质,如果在一个整数的 Series 中出现缺失,那么其类型会转变为 float64 ;
而如果在一个布尔类型的序列中出现缺失,那么其类型就会转为 object 而不是 bool :

pd.Series([1, np.nan]).dtype
dtype('float64')
pd.Series([True, False, np.nan]).dtype
dtype('O')

2. Nullable类型的性质

从字面意义上看 Nullable 就是可空的,言下之意就是序列类型不受缺失值的影响。例如,在上述三个
Nullable 类型中存储缺失值,都会转为 pandas 内置的 pd.NA:

pd.Series([np.nan, 1], dtype = 'Int64') # "i"是大写的
0    <NA>
1       1
dtype: Int64
pd.Series([np.nan, True], dtype = 'boolean')
0    <NA>
1    True
dtype: boolean
pd.Series([np.nan, 'my_str'], dtype = 'string')
0      <NA>
1    my_str
dtype: string

在 Int 的序列中,返回的结果会尽可能地成为 Nullable 的类型:

pd.Series([np.nan, 0], dtype = 'Int64') + 1
0    <NA>
1       1
dtype: Int64
pd.Series([np.nan, 0], dtype = 'Int64') == 0
0    <NA>
1    True
dtype: boolean
pd.Series([np.nan, 0], dtype = 'Int64') * 0.5 # 只能是浮点
0    NaN
1    0.0
dtype: float64

对于 boolean 类型的序列而言,其和 bool 序列的行为主要有两点区别:< br/>
第一点是带有缺失的布尔列表无法进行索引器中的选择,而 boolean 会把缺失值看作 False :

s = pd.Series(['a', 'b'])
s_bool = pd.Series([True, np.nan])
s_boolean = pd.Series([True, np.nan]).astype('boolean')
s[s_boolean]
0    a
dtype: object

第二点是在进行逻辑运算时,bool 类型在缺失处返回的永远是False ,而boolean 会根据逻辑运算是否能确
定唯一结果来返回相应的值。那什么叫能否确定唯一结果呢?举个简单例子:True | pd.NA 中无论缺失值为
什么值,必然返回True ;False | pd.NA 中的结果会根据缺失值取值的不同而变化,此时返回pd.NA ;False
& pd.NA 中无论缺失值为什么值,必然返回False 。

s_boolean & True
0    True
1    <NA>
dtype: boolean
s_boolean | True
0    True
1    True
dtype: boolean
~s_boolean # 取反操作同样是无法唯一地判断缺失结果
0    False
1     <NA>
dtype: boolean
#一般在实际数据处理时,可以在数据集读入后,先通过convert_dtypes 转为Nullable 类型:
df = pd.read_csv('data/learn_pandas.csv')
df = df.convert_dtypes()
df.dtypes
School          string
Grade           string
Name            string
Gender          string
Height         float64
Weight           Int64
Transfer        string
Test_Number      Int64
Test_Date       string
Time_Record     string
dtype: object

3. 缺失数据的计算和分组

当调用函数 sum, prob 使用加法和乘法的时候,缺失数据等价于被分别视作0和1,即不改变原来的计算结果:

s = pd.Series([2,3,np.nan,4,5])
s.sum()
14.0
s.prod()
120.0
# 当使用累计函数时,会自动跳过缺失值所处的位置:
s.cumsum()
0     2.0
1     5.0
2     NaN
3     9.0
4    14.0
dtype: float64

四、练习

后续再进行补救学习!



猜你喜欢

转载自blog.csdn.net/weixin_48626051/article/details/112164108