Operations on Null Values in Pandas

本文介绍了如何使用Pandas处理缺失值,包括isnull()和notnull()方法的应用,以及dropna()函数的灵活用法,如全行/列删除、指定轴和阈值控制。同时涵盖了填充空值的不同策略,如用零替换、向前/向后填充以及自定义值。
摘要由CSDN通过智能技术生成

Detecting null values

pandas data structures have two useful methods for detecting null data:
isnull() and notnull(). Either one will return a boolean mask over the data.
For example:
在这里插入图片描述

在这里插入图片描述

boolean masks can be used directly as a Series or DataFrame index:

在这里插入图片描述

Dropping null values

dropna() can remove null values, the result is straightforward.

在这里插入图片描述
For a DataFrame, there are more options. Considering the following DataFrame.
在这里插入图片描述
we cannot drop a single value from a DataFrame, we can only drop full rows or full columns.Depending on the application, you want one or the other.
在这里插入图片描述
Alternatively, you can drop NA value along a different axis, axis=1 drops all columns containing a null value.
在这里插入图片描述
But this drops good data as well. We might rather be interested in rows or columns with all NA values,or a majority of NA values.This can be specified through the how or thresh parameters, which allow fine control of number of nulls to allow through.
The default is ‘how=any’, such that any row or column containing a null value will be dropped.
在这里插入图片描述
在这里插入图片描述
For finer-grained control, the thresh parameter can specify a minimum number of non-null values for the column/row to be kept.
在这里插入图片描述

Filling null values

Sometimes rather than dropping NA value, we’d rather replace them with a valid value. This value might be a single number like zero, or it might be some sort of imputation from good values.

Consider the following Series:

在这里插入图片描述
We can fill NA entries with a single value,such as zero:
在这里插入图片描述
or we can specify a forward-fill to propagate the previous value:
在这里插入图片描述

or specify a back-fill
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值