博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Pandas dropna()–从DataFrame中删除Null / NA值
阅读量:2531 次
发布时间:2019-05-11

本文共 5188 字,大约阅读时间需要 17 分钟。

1. Pandas DataFrame dropna()函数 (1. Pandas DataFrame dropna() Function)

Pandas DataFrame dropna() function is used to remove rows and columns with Null/NaN values. By default, this function returns a new DataFrame and the source DataFrame remains unchanged.

Pandas DataFrame dropna()函数用于删除具有Null / NaN值的行和列。 默认情况下,此函数返回一个新的DataFrame,而源DataFrame保持不变。

We can create null values using None, pandas.NaT, and numpy.nan variables.

我们可以使用None,pandas.NaT和numpy.nan变量创建空值。

The dropna() function syntax is:

dropna()函数的语法为:

dropna(self, axis=0, how="any", thresh=None, subset=None, inplace=False)
  • axis: possible values are {0 or ‘index’, 1 or ‘columns’}, default 0. If 0, drop rows with null values. If 1, drop columns with missing values.

    axis :可能的值为{0或'index',1或'columns'},默认值为0。如果为0,则删除具有空值的行。 如果为1,则删除缺少值的列。
  • how: possible values are {‘any’, ‘all’}, default ‘any’. If ‘any’, drop the row/column if any of the values is null. If ‘all’, drop the row/column if all the values are missing.

    如何 :可能的值为{'any','all'},默认值为“ any”。 如果为“ any”,则在任何值为null的情况下删除行/列。 如果为“全部”,则在所有值均缺失的情况下删除行/列。
  • thresh: an int value to specify the threshold for the drop operation.

    thresh :一个整数值,用于指定放置操作的阈值。
  • subset: specifies the rows/columns to look for null values.

    子集 :指定要查找空值的行/列。
  • inplace: a boolean value. If True, the source DataFrame is changed and None is returned.

    inplace :布尔值。 如果为True,则更改源DataFrame,并返回None。

Let’s look at some examples of using dropna() function.

让我们看一些使用dropna()函数的示例。

2.熊猫使用所有Null / NaN / NaT值删除所有行 (2. Pandas Drop All Rows with any Null/NaN/NaT Values)

This is the default behavior of dropna() function.

这是dropna()函数的默认行为。

import pandas as pdimport numpy as npd1 = {'Name': ['Pankaj', 'Meghna', 'David', 'Lisa'], 'ID': [1, 2, 3, 4], 'Salary': [100, 200, np.nan, pd.NaT],      'Role': ['CEO', None, pd.NaT, pd.NaT]}df = pd.DataFrame(d1)print(df)# drop all rows with any NaN and NaT valuesdf1 = df.dropna()print(df1)

Output:

输出:

Name  ID Salary Role0  Pankaj   1    100  CEO1  Meghna   2    200  None2   David   3    NaN  NaT3    Lisa   4    NaT  NaT     Name  ID Salary Role0  Pankaj   1    100  CEO

3.删除所有缺少任何值的列 (3. Drop All Columns with Any Missing Value)

We can pass axis=1 to drop columns with the missing values.

我们可以传递axis=1来删除缺少值的列。

df1 = df.dropna(axis=1)print(df1)

Output:

输出:

Name  ID0  Pankaj   11  Meghna   22   David   33    Lisa   4

4.仅当所有值都为空时才删除行/列 (4. Drop Row/Column Only if All the Values are Null)

import pandas as pdimport numpy as npd1 = {'Name': ['Pankaj', 'Meghna', 'David', pd.NaT], 'ID': [1, 2, 3, pd.NaT], 'Salary': [100, 200, np.nan, pd.NaT],      'Role': [np.nan, np.nan, pd.NaT, pd.NaT]}df = pd.DataFrame(d1)print(df)df1 = df.dropna(how='all')print(df1)df1 = df.dropna(how='all', axis=1)print(df1)

Output:

输出:

Name   ID Salary Role0  Pankaj    1    100  NaT1  Meghna    2    200  NaT2   David    3    NaN  NaT3     NaT  NaT    NaT  NaT     Name ID Salary Role0  Pankaj  1    100  NaT1  Meghna  2    200  NaT2   David  3    NaN  NaT     Name   ID Salary0  Pankaj    1    1001  Meghna    2    2002   David    3    NaN3     NaT  NaT    NaT

5.超过空值的阈值时,DataFrame删除行/列 (5. DataFrame Drop Rows/Columns when the threshold of null values is crossed)

import pandas as pdimport numpy as npd1 = {'Name': ['Pankaj', 'Meghna', 'David', pd.NaT], 'ID': [1, 2, pd.NaT, pd.NaT], 'Salary': [100, 200, np.nan, pd.NaT],      'Role': [np.nan, np.nan, pd.NaT, pd.NaT]}df = pd.DataFrame(d1)print(df)df1 = df.dropna(thresh=2)print(df1)

Output:

输出:

Name   ID Salary Role0  Pankaj    1    100  NaT1  Meghna    2    200  NaT2   David  NaT    NaN  NaT3     NaT  NaT    NaT  NaT     Name ID Salary Role0  Pankaj  1    100  NaT1  Meghna  2    200  NaT

The rows with 2 or more null values are dropped.

具有2个或更多空值的行将被删除。

6.定义标签以查找空值 (6. Define Labels to look for null values)

import pandas as pdimport numpy as npd1 = {'Name': ['Pankaj', 'Meghna', 'David', 'Lisa'], 'ID': [1, 2, 3, pd.NaT], 'Salary': [100, 200, np.nan, pd.NaT],      'Role': ['CEO', np.nan, pd.NaT, pd.NaT]}df = pd.DataFrame(d1)print(df)df1 = df.dropna(subset=['ID'])print(df1)

Output:

输出:

Name   ID Salary Role0  Pankaj    1    100  CEO1  Meghna    2    200  NaN2   David    3    NaN  NaT3    Lisa  NaT    NaT  NaT     Name ID Salary Role0  Pankaj  1    100  CEO1  Meghna  2    200  NaN2   David  3    NaN  NaT

We can specify the index values in the subset when dropping columns from the DataFrame.

当从DataFrame中删除列时,我们可以在子集中指定索引值。

df1 = df.dropna(subset=[1, 2], axis=1)print(df1)

Output:

输出:

Name   ID0  Pankaj    11  Meghna    22   David    33    Lisa  NaT

The ‘ID’ column is not dropped because the missing value is looked only in index 1 and 2.

因为缺少的值仅在索引1和2中查找,所以不会删除“ ID”列。

7.放行,NA不存在 (7. Dropping Rows with NA inplace)

We can pass inplace=True to change the source DataFrame itself. It’s useful when the DataFrame size is huge and we want to save some memory.

我们可以传递inplace inplace=True来更改源DataFrame本身。 当DataFrame很大并且我们想节省一些内存时,这很有用。

import pandas as pdd1 = {'Name': ['Pankaj', 'Meghna'], 'ID': [1, 2], 'Salary': [100, pd.NaT]}df = pd.DataFrame(d1)print(df)df.dropna(inplace=True)print(df)

Output:

输出:

Name  ID  Salary0  Pankaj   1   100.01  Meghna   2     NaN     Name  ID  Salary0  Pankaj   1   100.0

8.参考 (8. References)

翻译自:

转载地址:http://xfqzd.baihongyu.com/

你可能感兴趣的文章
java学习笔记④MySql数据库--01/02 database table 数据的增删改
查看>>
两台电脑如何实现共享文件
查看>>
组合模式Composite
查看>>
程序员最想得到的十大证件,你最想得到哪个?
查看>>
我的第一篇CBBLOGS博客
查看>>
【MyBean调试笔记】接口的使用和清理
查看>>
07 js自定义函数
查看>>
jQueru中数据交换格式XML和JSON对比
查看>>
form表单序列化后的数据转json对象
查看>>
[PYTHON]一个简单的单元測试框架
查看>>
iOS开发网络篇—XML数据的解析
查看>>
[BZOJ4303]数列
查看>>
一般处理程序在VS2012中打开问题
查看>>
C语言中的++和--
查看>>
thinkphp3.2.3入口文件详解
查看>>
POJ 1141 Brackets Sequence
查看>>
Ubuntu 18.04 root 使用ssh密钥远程登陆
查看>>
Servlet和JSP的异同。
查看>>
虚拟机centOs Linux与Windows之间的文件传输
查看>>
ethereum(以太坊)(二)--合约中属性和行为的访问权限
查看>>