dropna()删除缺失值_python数据分析与挖掘(二十一)--- Pandas高级处理-缺失值处理...

本文介绍了在Pandas中如何处理缺失值NaN,包括使用`pd.isnull()`和`pd.notnull()`进行判断,通过`dropna()`删除含有缺失值的行,以及使用`fillna()`进行填充,同时讲解了如何处理非标准缺失值标记如'?',并提供了实际操作示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

高级处理-缺失值处理

cd950928819ecaaef9e0c856cd1a47b7.png

1 如何处理nan

对于NaN的数据,在numpy中我们是如何处理的?

在pandas中我们处理起来非常容易

判断数据是否为NaN:pd.isnull(df), pd.notnull(df)

处理方式:

存在缺失值nan, 并且是np.nan:

1 删除存在缺失值的:dropna(axis='rows')

注:不会修改原数据,需要接受返回值

2 替换缺失值:fillna(value, inplace=True)

value:替换成的值

inplace:

True:会修改原数据

False:不替换修改原数据,生成新的对象

不是缺失值nan,有默认标记的

2 缺失值处理实例

电影数据文件获取

# 读取电影数据
movie = pd.read_csv("./IMDB/IMDB-Movie-Data.csv")
989    Martyrs    Horror    A young woman's quest for revenge against the ...    Pascal Laugier    Morjana Alaoui, Mylène Jampanoï, Catherine Bég...    2008    99    7.1    63785    NaN    89.0
990    Selma    Biography,Drama,History    A chronicle of Martin Luther King's campaign t...    Ava DuVernay    David Oyelowo, Carmen Ejogo, Tim Roth, Lorrain...    2014    128    7.5    67637    52.07    NaN

1 判断缺失值是否存在

pd.notnull()

pd.notnull(movie)
Rank    Title    Genre    Description    Director    Actors    Year    Runtime (Minutes)    Rating    Votes    Revenue (Millions)    Metascore
0    True    True    True    True    True    True    True    True    True    True    True    True
1    True    True    True    True    True    True    True    True    True    True    True    True
2    True    True    True    True    True    True    True    True    True    True    True    True
3    True    True    True    True    True    True    True    True    True    True    True    True
4    True    True    True    True    True    True    True    True    True    True    True    True
5    True    True    True    True    True    True    True    True    True    True    True    True
6    True    True    True    True    True    True    True    True    True    True    True    True
7    True    True    True    True    True    True    True    True    True    True    False    True

2 存在缺失值nan,并且是np.nan

1 删除

pandas删除缺失值,使用dropna的前提是,缺失值的类型必须是np.nan

# 不修改原数据
movie.dropna()
 
# 可以定义新的变量接受或者用原来的变量名
movie = movie.dropna()

2 替换缺失值

# 替换存在缺失值的样本的两列
# 替换填充平均值,中位数
movie['Revenue (Millions)'].fillna(movie['Revenue (Millions)'].mean(), inplace=True)
movie['Metascore'].fillna(movie['Metascore'].mean(), inplace=True)

结果:

989    Martyrs    Horror    A young woman's quest for revenge against the ...    Pascal Laugier    Morjana Alaoui, Mylène Jampanoï, Catherine Bég...    2008    99    7.1    63785    82.956376    89.000000
989    990    Selma    Biography,Drama,History    A chronicle of Martin Luther King's campaign t...    Ava DuVernay    David Oyelowo, Carmen Ejogo, Tim Roth, Lorrain...    2014    128    7.5    67637    52.070000    58.985043

3 不是缺失值nan,有默认标记的

数据是这样的:

204f20eabe57d457452cb9c361e4adca.png
wis = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data")
name = ["Sample code number", "Clump Thickness", "Uniformity of Cell Size", "Uniformity of Cell Shape", "Marginal Adhesion", "Single Epithelial Cell Size", "Bare Nuclei", "Bland Chromatin", "Normal Nucleoli", "Mitoses", "Class"]

处理思路分析:

1 先替换'?'为np.nan

df.replace(to_replace=, value=)

to_replace:替换前的值

value:替换后的值

# 把一些其它值标记的缺失值,替换成np.nan
wis = wis.replace(to_replace='?', value=np.nan)

2 在进行缺失值的处理

# 删除
wis = wis.dropna()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值