Python 如何在 Pandas 中删除空值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39436018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:16:08 来源:igfitidea点击:
How to drop null values in Pandas?
提问by Alex Ermolaev
I try to drop null values of column 'Age' in dataframe, which consists of float values, but it doesn't work. I tried
我尝试删除由浮点值组成的数据框中“年龄”列的空值,但它不起作用。我试过
data.dropna(subset=['Age'], how='all')
data['Age'] = data['Age'].dropna()
data=data.dropna(axis=1,how='all')
It works for other columns but not for 'Age'
它适用于其他列,但不适用于“年龄”
Pclass Fare Age Sex
0 3 7.2500 22.0 1
1 1 71.2833 38.0 0
2 3 7.9250 26.0 0
3 1 53.1000 35.0 0
4 3 8.0500 35.0 1
5 3 8.4583 NaN 1
6 1 51.8625 54.0 1
7 3 21.0750 2.0 1
回答by DeepSpace
data.dropna(subset=['Age'])
would work, but you should either set inplace=True
or assign it back to data
:
data.dropna(subset=['Age'])
会起作用,但您应该inplace=True
将其设置或分配回data
:
data = data.dropna(subset=['Age'])
or
或者
data.dropna(subset=['Age'], inplace=True)