pandas || df.dropna() 缺失值删除操作

作者:一个还在挣扎的码农 时间:2023-07-10 06:51:06 

df.dropna()函数用于删除dataframe数据中的缺失数据,即 删除NaN数据.

官方函数说明:


DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)
Remove missing values.
See the User Guide for more on which values are considered missing,
and how to work with missing data.
Returns
DataFrame
DataFrame with NA entries dropped from it.

参数说明:

Parameters说明
axis0为行 1为列,default 0,数据删除维度
how{‘any', ‘all'}, default ‘any',any:删除带有nan的行;all:删除全为nan的行
threshint,保留至少 int 个非nan行
subsetlist,在特定列缺失值处理
inplacebool,是否修改源文件

测试:


>>>df = pd.DataFrame({"name": ['Alfred', 'Batman', 'Catwoman'],
         "toy": [np.nan, 'Batmobile', 'Bullwhip'],
         "born": [pd.NaT, pd.Timestamp("1940-04-25"),
             pd.NaT]})

>>>df
   name    toy    born
0  Alfred    NaN    NaT
1  Batman Batmobile 1940-04-25
2 Catwoman  Bullwhip    NaT

删除至少缺少一个元素的行:


>>>df.dropna()
  name    toy    born
1 Batman Batmobile 1940-04-25

删除至少缺少一个元素的列:


>>>df.dropna(axis=1)
   name
0  Alfred
1  Batman
2 Catwoman

删除所有元素丢失的行:


>>>df.dropna(how='all')
   name    toy    born
0  Alfred    NaN    NaT
1  Batman Batmobile 1940-04-25
2 Catwoman  Bullwhip    NaT

只保留至少2个非NA值的行:


>>>df.dropna(thresh=2)
   name    toy    born
1  Batman Batmobile 1940-04-25
2 Catwoman  Bullwhip    NaT

从特定列中查找缺少的值:


>>>df.dropna(subset=['name', 'born'])
   name    toy    born
1  Batman Batmobile 1940-04-25

修改原数据:


>>>df.dropna(inplace=True)
>>>df
  name    toy    born
1 Batman Batmobile 1940-04-25

以上。

补充:Pandas 之Dropna滤除缺失数据

约定:


import pandas as pd
import numpy as np
from numpy import nan as NaN

滤除缺失数据

pandas的设计目标之一就是使得处理缺失数据的任务更加轻松些。pandas使用NaN作为缺失数据的标记。

使用dropna使得滤除缺失数据更加得心应手。

一、处理Series对象

通过**dropna()**滤除缺失数据:


se1=pd.Series([4,NaN,8,NaN,5])
print(se1)
se1.dropna()

代码结果:


0  4.0
1  NaN
2  8.0
3  NaN
4  5.0
dtype: float64
0  4.0
2  8.0
4  5.0
dtype: float64

通过布尔序列也能滤除:


se1[se1.notnull()]

代码结果:


0  4.0
2  8.0
4  5.0
dtype: float64

二、处理DataFrame对象

处理DataFrame对象比较复杂,因为你可能需要丢弃所有的NaN或部分NaN。


df1=pd.DataFrame([[1,2,3],[NaN,NaN,2],[NaN,NaN,NaN],[8,8,NaN]])
df1

代码结果:


012
01.02.03.0
1NaNNaN2.0
2NaNNaNNaN
38.08.0NaN

默认滤除所有包含NaN:


df1.dropna()

代码结果:


012
01.02.03.0

传入**how=‘all'**滤除全为NaN的行:


df1.dropna(how='all')

代码结果:


012
01.02.03.0
1NaNNaN2.0
38.08.0NaN

传入axis=1滤除列:


df1[3]=NaN
df1

代码结果:


0123
01.02.03.0NaN
1NaNNaN2.0NaN
2NaNNaNNaNNaN
38.08.0NaNNaN

df1.dropna(axis=1,how="all")

代码结果:


012
01.02.03.0
1NaNNaN2.0
2NaNNaNNaN
38.08.0NaN

传入thresh=n保留至少有n个非NaN数据的行:


df1.dropna(thresh=1)

代码结果:


0123
01.02.03.0NaN
1NaNNaN2.0NaN
38.08.0NaNNaN

df1.dropna(thresh=3)

代码结果:


0123
01.02.03.0NaN

以上为个人经验,希望能给大家一个参考,也希望大家多多支持asp之家。

来源:https://blog.csdn.net/qq_43188358/article/details/108335776

标签:pandas,df.dropna,缺失值
0
投稿

猜你喜欢

  • 讲解MaxDB数据库和MySQL的数据库的主要差别

    2012-02-25 20:04:34
  • 如何使用Script Encoder?

    2010-06-05 12:42:00
  • SQL Server数据库搭建农村信息化的方案

    2009-01-23 14:16:00
  • PHP getDocNamespaces()函数讲解

    2023-06-13 22:19:06
  • javascript用回车键实现Tab键功能

    2009-07-05 18:40:00
  • ASP 操作cookies的方法

    2011-03-10 11:24:00
  • 元素层叠级别及z-index剖析

    2008-07-22 12:03:00
  • 两侧背景自动延伸的CSS实现方法

    2010-02-24 09:42:00
  • 破解加密的网页代码方法

    2010-03-16 12:35:00
  • 一个ASP(VBScript)简单SQL语句构建“类”

    2008-03-12 07:08:00
  • SQL SERVER 2005中的同步复制技术

    2009-01-05 13:44:00
  • 实现文字放大效果Javascript源码

    2010-03-17 20:46:00
  • css基础教程之序曲

    2008-07-23 12:40:00
  • 分析与比较五种MySQL数据库可靠性方案

    2009-07-30 08:25:00
  • 让XML在ASP中发挥其长处

    2008-01-16 19:07:00
  • PHP连接MySQL数据的操作要点

    2023-06-20 09:31:16
  • 用VB生成DLL封装ASP代码一个例子:连接access数据库等

    2008-04-07 13:06:00
  • PHP htmlentities()函数用法讲解

    2023-06-04 14:47:30
  • javascript新闻图片轮换类

    2009-01-09 12:57:00
  • 用户体验的误解

    2008-07-15 12:31:00
  • asp之家 网络编程 m.aspxhome.com