Python中的空值和缺失值的处理

空值在python中一般表现为以下几种形式:
(1)None
(2)“ ”
(3)NaN

在数据预处理中,一般通过以下几种方法对空值进行判断:
1、对于前两种空值,直接“=”就可以了
2、对于第三种,NaN(not a number),在数学表示上表示一个无法表示的数,这里一般还会有另一个表述inf,inf和nan的不同在于,inf是一个超过浮点表示范围的浮点数(其本质仍然是一个数,只是他无穷大,因此无法用浮点数表示,比如1/0),而nan则一般表示一个非浮点数(比如无理数):其判断方式为:

(1)、numpy 里的NaN值的判断

import numpy as np
a = np.NaN
print(type(a))
#打印输出
float
print(a == np.NaN)
print(a is np.NaN)
#打印输出
False
True

用上面两种方法判断NaN值都不太准确,最可靠的方法为math里的判断方法

print(np.isnan(a))
#打印输出
True

(2)、pandas里NaN值处理
空值:在pandas中表示为“”
缺失值:在dataFrame中为nan或者naT(缺失时间),在series中为none或者nan

pandas中判断值是否为nan值得方法有:

pd.isnull(np.nan)
pd.notnull()

与缺失值有关的函数

df.dropna():删除NaN值
df.fillna():填充NaN值
df.isnull():判断是否为NaN值
df.isna():判断是否为NaN值

删除dataFrame中含有空值的行或者列:

DataFrame.dropna(axis=0, how='any', thresh=None, subset=None,inplace=False)

axis:维度,axis=0表示index行,axis=1表示columns列,默认为0
how:"all"表示这一行或列中的元素全部缺失(为nan)才删除这一行或列,"any"表示这一行或列中只要有元素缺失,就删除这一行或列
thresh:一行或一列中至少出现了thresh个才删除。
subset:在某些列的子集中选择出现了缺失值的列删除,不在子集中的含有缺失值得列或行不会删除(有axis决定是行还是列)
inplace:刷选过缺失值得新数据是存为副本还是直接在原数据上进行修改。

举例说明:

import numpy as np
import pandas as pd

def createDataFrame():
    d = {
        'a':[1,2,3,4,5],
        'b':[np.nan,4,5,6,0],
        'c':[1,2,3,6,7],
        'd':[5,3,2,4,5],
        'e':[6,7,4,5,8]
    }
    df = pd.DataFrame(d)
    result = df.dropna()
    print(result)

if __name__ == '__main__':
    createDataFrame()
#默认情况下,按行删除,只要有空值就会删除含空值的行
#输出结果如下:
   a    b  c  d  e
1  2  4.0  2  3  7
2  3  5.0  3  2  4
3  4  6.0  6  4  5
4  5  0.0  7  5  8
result = df.dropna(axis=1)
#按列删除,删除含有nan的列,打印结果如下:
   a  c  d  e
0  1  1  5  6
1  2  2  3  7
2  3  3  2  4
3  4  6  4  5
4  5  7  5  8
result = df.dropna(how='all') #默认按行
# data.dropna(how='all',axis=1) 按列
#所有行值全是nan时才删除,打印结果为:
   a    b  c  d  e
0  1  NaN  1  5  6
1  2  4.0  2  3  7
2  3  5.0  3  2  4
3  4  6.0  6  4  5
4  5  0.0  7  5  8
###################################
a = {
        'a': [np.nan, np.nan, np.nan],
        'b': [np.nan, np.nan, np.nan],
        'c': [np.nan, np.nan, np.nan]

    }

    df = pd.DataFrame(a)
    result = df.dropna(axis=0,thresh=2)
    print(result)
#打印输出:
#Empty DataFrame
#Columns: [a, b, c]
#Index: []
result = df.dropna(thresh=2)
#至少含有两个缺失值才删除
    a = {
        'a': [np.nan, np.nan, 4],
        'b': [np.nan, 2, 5],
        'c': [9,      3, 6]
    }
    df = pd.DataFrame(a)
    result = df.dropna(thresh=2)
#打印输出:默认按行删除
     a    b  c
1  NaN  2.0  3
2  4.0  5.0  6
result = df.dropna(subset=['a','b'])
#删除subset中含有缺失值的行或列,默认为行,打印结果如下:
   a    b  c  d  e
1  2  2.0  2  3  7
2  3  3.0  3  2  4
3  4  6.0  6  4  5
4  5  0.0  7  5  8

补充inf:

print(np.inf == np.inf)
print(np.inf == np.inf+10)
#打印输出
True
True
#即inf加上任意一个数还是inf

作者:一笑乘风凉
链接:https://www.jianshu.com/p/4965940b488b
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 5
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值