pandas 对缺失值的处理

被认为是缺失值的值

因为计算速度和便利性的原因,NaN是默认的缺失值符号。在许多情况下,python的None我们也希望被认作是缺失值或者不合法值或者NA

Note: 如果想把inf和-inf认为是NA,那么可以设置: pandas.options.mode.use_inf_as_na = True.

构造一个dataframe

df = pd.DataFrame(
    np.random.randn(5,3),
    index=['a','c','e','f','h'],
    columns=['one','two','three']
)

df['four'] = 'bar'
df['five'] = df['one']>0
print(df)
        one       two     three four   five
a -1.578845  0.140311  0.810458  bar  False
c  1.232962  0.356917  1.986505  bar   True
e  2.878537 -1.184566  1.793575  bar   True
f -0.520843 -2.267867 -0.080532  bar  False
h  0.886857 -1.417980 -0.401882  bar   True
df2 = df.reindex(list("abcdefgh"))
print(df2)

reindex之后,原来的df没有的数据会被补充为NaN

        one       two     three four   five
a -1.578845  0.140311  0.810458  bar  False
b       NaN       NaN       NaN  NaN    NaN
c  1.232962  0.356917  1.986505  bar   True
d       NaN       NaN       NaN  NaN    NaN
e  2.878537 -1.184566  1.793575  bar   True
f -0.520843 -2.267867 -0.080532  bar  False
g       NaN       NaN       NaN  NaN    NaN
h  0.886857 -1.417980 -0.401882  bar   True
pd.isna(df2['one']) #等价于 df2['one'].isna()

a    False
b     True
c    False
d     True
e    False
f    False
g     True
h    False
Name: one, dtype: bool

同理,还有pd.nona()

Warning: 在pythong和numpy中,nan并不是相等的,而None是相等的。因此,pandas或者numpy中np.nan != np.nan,并且将None视为np.nan.

None == None # True
np.nan == np.nan # False
None == np.nan #False

所以如下的操作并得不到什么有用的信息

df2['one'] == np.nan
#尽管在'one'列中有一半的NaN值,但是和np.nan并不相等。
a    False
b    False
c    False
d    False
e    False
f    False
g    False
h    False
Name: one, dtype: bool

整数类型和缺失值

因为NaN是一个浮点数(float类型),一列整数尽管只有一个缺失值都会被映射为浮点类型的数据(这是出于高性能的考虑)。但是pandas也提供了相应的数据类型,使得缺失值映射成为整型数据:

pd.Series([1,2,np.nan,4],dtype='Int64')
0       1
1       2
2    <NA>
3       4
dtype: Int64

Datatimes

在日期类型的数据中,NaT表示缺失值。

Inserting missing data 插入缺失值

在数字类型中,None会被解释为NaN,但是在非数字类型中,None就会被作为一个值。但是不论何时,np.nan永远都是NaN

s = pd.Series([1,2,3])
s[0] = None 
print(s)
0    NaN
1    2.0
2    3.0
dtype: float64
s = pd.Series(['a','b','c'])
s[0] = None 
s[1] = np.nan
print(s)

0    None
1     NaN
2       c
dtype: object

计算过程中的缺失值处理

  • 当对数据求和的时候,缺失值被认为是0
  • 如果所有的数据都是NA , 结果就是0
  • 累计计算方法例如累计求和cumsum()和累计乘cumprod()会忽略NA值。也可以不忽略 skipna=False.

填充缺失值

replace NA with a scalar value 用标量来替换

df.fillna(0) #对整个df的缺失值以0填充
df2['one'].fillna("missing") #对某列缺失值以“missing"来填充

fill gaps forward or backward 前向或者后向填充

df['one'].fillna(method = 'pad') #前向填充,用index小的数据填充index大的数据
#等价于
df['one'].ffill()
#还等价于
df['one'].fillna(method='ffill')
#还可以限制填充的数量
df['one'].fillna(method='pad',limit=1)

注意一定要写上形参的名字method,否则df.fillna('pad')将会以’pad’作为填充值。
同样的还有后向填充,即用后面的值来填充前面的值:bfill() or backfill()

用计算值来填充缺失值
例如,可以用每一列的均值来填充每一列的缺失数据

df.fillna(df.mean())

或者某一列的缺失值用某一列的均值来填充

df['one'].fillna(df['one'].mean())

还有一种高级的写法,能够实现一次性以多列的均值分别填充其缺失值

ddf.fillna(ddf.mean()['A':'B'])

Same result as above, but is aligning the ‘fill’ value which is a Series in this case.只不过填充值是一个Series对象。

舍弃缺失值

df.dropna() #默认为行,舍弃有缺失值的行
df.dropna(axis=1) #舍弃有缺失值的列
df['one'].dropna() #舍弃有缺失值的值
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值