import numpy as np
import pandas as pd
7.1 缺失值的统计和删除
7.1.1 缺失信息的统计
缺失数据可以使用isna或isnull来查看每个单元格是否缺失,通过和sum的组合计算出每列缺失值的比例
df = pd.read_csv('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 |
# 查看缺失的比例
# Height,Weight,Transfer 均有缺失值(空白->NaN)
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进行布尔索引
其中,isna会显示NaN的值,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组合(any类比or,all类比and)
对身高、体重、转系情况这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 |
7.1.2 缺失信息的删除
根据缺失值的大小、比例或其他特征来进行 行样本 或 列特征的删除,使用dropna来操作
dropna主要参数为轴方向axis(0,删除行)、 删除方式how、删除的非缺失值个数预支thresh(非缺失值没有达到这个数量的相应维度会被删除)、备选的删除子集subset,其中how主要有any和all两种参数可以选择
删除身高体重至少有一个缺失的行:
res = df.dropna(how = 'any', subset = ['Height', 'Weight'])
# 原先是200行,dropna删除到174行
res.shape
(174, 6)
# 删除超过15个缺失值的列(列,设为1)
# 非缺失值没有达到df.shape[0]-15数量的相应维度会被删除
res = df.dropna(1, thresh=df.shape[0]-15)
res.head()
# Height被删除
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 |
# 还可以用布尔索引完成
res = df.loc[df[['Height', 'Weight']].notna().all(1)]
res.shape
(174, 6)
# 是nan的数量小于15
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 |
7.2 缺失值的填充和插值
7.2.1 利用fillna进行填充
fillna有三个参数是常用的:value,method,limit
value为填充纸,可以是标量,也可以是索引到元素的字典映射;
method为填充方法,有用前面的元素填充ffill和用后面的元素填充bfill两种类型;
limit参数表示连续缺失值的最大填充次数
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
# value为标量
# (1.0+2.0)/2 = 1.5
s.fillna(s.mean())
a 1.5
a 1.0
a 1.5
b 1.5
c 2.0
d 1.5
dtype: float64
# 把对应索引所映射的NaN值填充为字典值
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
练一练
对一个序列以如下规则填充缺失值:如果单独出现的缺失值,就用前后均值填充,如果连续出现
的缺失值就不填充,即序列 [1, NaN, 3, NaN, NaN] 填充后为 [1, 2, 3, NaN, NaN],请利用 fillna函数实现。(提示:利用 limit 参数)
a = pd.Series([1, np.nan, 3, np.nan, np.nan])
a
0 1.0
1 NaN
2 3.0
3 NaN
4 NaN
dtype: float64
f = a.fillna(method = 'ffill', limit=1)
b = a.fillna(method = 'bfill', limit=1)
((f+b)/2).values
array([ 1., 2., 3., nan, nan])
7.2.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])
在群里问了,解答是,不处理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
# 和索引有关的线性插值,计算相应索引大小对应的值
# key value
# 0 0
# 1 1
# 10 10
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
s.interpolate(method = 'index')
2020-01-01 0.0
2020-01-02 1.0
2020-01-11 10.0
dtype: float64
7.3 Nullablue类型
7.3.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
虽然在对缺失序列或表格的元素进行比较操作的时候,np.nan的对应位置会返回False,但在使用equals函数进行两张表或两个序列的向同性检验时,会自动跳过两侧表都是缺失值的位置,直接返回True:
s1 = pd.Series([1, np.nan])
s2 = pd.Series([1,2])
s3 = pd.Series([1, np.nan])
s1 == 1
0 True
1 False
dtype: bool
s1.equals(s2)
False
s1.equals(s3)
True
在时间序列的对象中,pandas利用的pd.NaT来指代缺失值,作用和np.nan是一致的
# timedelta中用NaT代替numpy中的np.nan
pd.to_timedelta(['30s', np.nan])
TimedeltaIndex(['0 days 00:00:30', NaT], dtype='timedelta64[ns]', freq=None)
# Datetime中的NaT
pd.to_datetime(['20200101', np.nan])
DatetimeIndex(['2020-01-01', 'NaT'], dtype='datetime64[ns]', freq=None)
引入pd.NaT来表示时间对象中的缺失:在pandas中可以看到object类型的对象,object是一种混杂对象类型,如果出现多个类型的元素同时存储在Series中它的类型就会变成object
而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')
7.3.2 Nullable 类型的性质
序列类型不受缺失值的影响
下面三个Nullable类型中存储缺失值,都会转为pandas内置的pd.NA
需要指定dtype类型,否则统一为object
pd.Series([np.nan, 1], dtype = 'Int64')
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序列的行为有两点区别:
- 带有缺失的布尔列表无法进行索引器中的选择,而boolean会把缺失值看做False
s = pd.Series(['a','b'])
s_bool = pd.Series([True, np.nan])
# 会报错ValueError: Cannot mask with non-boolean array containing NA / NaN values
s[s_bool]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-41-d8b971864d8a> in <module>
1 s = pd.Series(['a','b'])
2 s_bool = pd.Series([True, np.nan])
----> 3 s[s_bool]
E:\Anaconda\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
899 key = list(key)
900
--> 901 if com.is_bool_indexer(key):
902 key = check_bool_indexer(self.index, key)
903 key = np.asarray(key, dtype=bool)
E:\Anaconda\lib\site-packages\pandas\core\common.py in is_bool_indexer(key)
132 na_msg = "Cannot mask with non-boolean array containing NA / NaN values"
133 if isna(key).any():
--> 134 raise ValueError(na_msg)
135 return False
136 return True
ValueError: Cannot mask with non-boolean array containing NA / NaN values
# 直接跳过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
0 True
1 <NA>
dtype: boolean
s_boolean & True
0 True
1 <NA>
dtype: boolean
# np.nan 或True ->True
s_boolean | True
0 True
1 True
dtype: boolean
# 取反操作无法唯一地判断缺失结果
-s_boolean
0 False
1 <NA>
dtype: boolean
# 一般在实际数据处理时,可以在数据集读入后,先通过 convert_dtypes 转为 Nullable 类型:
df = pd.read_csv('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
7.3.3 缺失数据的计算和分组
当调用函数sum, prod 使用加法和乘法的时候,缺失数据等价于被分别视作0和1,即不改变原来的计算结果
s = pd.Series([2, 3, np.nan, 4, 5])
# 被当做0处理
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
当进行单个标量运算时, 除了np.nan0 和1np.nan这两种情况为确定的值之外,所有运算结果全为缺失(pd.NA的行为与此一致), np.nan在比较操作时一定返回False, 而pd.NA返回pd.NA
np.nan == 0
False
pd.NA == 0
<NA>
np.nan > 0
False
pd.NA > 0
<NA>
np.nan + 1
nan
np.log(np.nan)
nan
np.add(np.nan , 1)
nan
# 确定值
np.nan ** 0
1.0
pd.NA ** 0
1
1 ** np.nan
1.0
1 ** pd.NA
1
diff, pct_change两个函数虽然功能相似, 但对于缺失的处理不同, 前者凡是参与缺失计算的部分全部设为了缺失值, 后者缺失值位置会被设为0%的变化率:
s
0 2.0
1 3.0
2 NaN
3 4.0
4 5.0
dtype: float64
# 和前一个元素做差
s.diff()
0 NaN
1 1.0
2 NaN
3 NaN
4 1.0
dtype: float64
# 增长率, nan设为0%的变化率
s.pct_change()
0 NaN
1 0.500000
2 0.000000
3 0.333333
4 0.250000
dtype: float64
对于一些函数,缺失可以做为一个类别处理,例如在groupby, get_dummies中可以设置相应的参数来进行增加缺失类别
df_nan = pd.DataFrame({'category': ['a','a','b', np.nan, np.nan],
'value': [1,3,5,7,9]})
df_nan
category | value | |
---|---|---|
0 | a | 1 |
1 | a | 3 |
2 | b | 5 |
3 | NaN | 7 |
4 | NaN | 9 |
# a (1+3)/2=2, b 5, NaN (7+9)/2=8
# 缺失值的计算
df_nan.groupby('category', dropna=False)['value'].mean()
category
a 2
b 5
NaN 8
Name: value, dtype: int64
# 缺失值的统计
# 是把类别特征转为指示变量,把属于a,b,NaN的指示值标出
pd.get_dummies(df_nan.category, dummy_na=True)
a | b | NaN | |
---|---|---|---|
0 | 1 | 0 | 0 |
1 | 1 | 0 | 0 |
2 | 0 | 1 | 0 |
3 | 0 | 0 | 1 |
4 | 0 | 0 | 1 |