• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

python 搞代码 4年前 (2022-01-08) 29次浏览 已收录 0个评论
文章目录[隐藏]

这篇文章主要介绍了pandas || df.dropna() 缺失值删除操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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 说明
axis 0为行 1为列,default 0,数据删除维度
how {‘any’, ‘all’}, default ‘any’,any:删除带有nan的行;all:删除全为nan的行
thresh int,保留至少 int 个非nan行
subset list,在特定列缺失值处理
inplace bool,是否修改源文件

测试:

 >>>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

代码结果:

来源gao@daima#com搞(%代@#码网

0 1 2
0 1.0 2.0 3.0
1 NaN NaN 2.0
2 NaN NaN NaN
3 8.0 8.0 NaN

默认滤除所有包含NaN:

 df1.dropna()

代码结果:

0 1 2
0 1.0 2.0 3.0

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

 df1.dropna(how='all')

代码结果:

0 1 2
0 1.0 2.0 3.0
1 NaN NaN 2.0
3 8.0 8.0 NaN

传入axis=1滤除列:

 df1[3]=NaN df1

代码结果:

0 1 2 3
0 1.0 2.0 3.0 NaN
1 NaN NaN 2.0 NaN
2 NaN NaN NaN NaN
3 8.0 8.0 NaN NaN
 df1.dropna(axis=1,how="all")

代码结果:

0 1 2
0 1.0 2.0 3.0
1 NaN NaN 2.0
2 NaN NaN NaN
3 8.0 8.0 NaN

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

 df1.dropna(thresh=1)

代码结果:

0 1 2 3
0 1.0 2.0 3.0 NaN
1 NaN NaN 2.0 NaN
3 8.0 8.0 NaN NaN
 df1.dropna(thresh=3)

代码结果:

0 1 2 3
0 1.0 2.0 3.0 NaN

以上就是pandas || df.dropna() 缺失值删除操作的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:pandas || df.dropna() 缺失值删除操作

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址