Python Pandas - 过滤无值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45117272/
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 16:42:35  来源:igfitidea点击:

Pandas - Filtering None Values

pythonpandasdataframe

提问by Shadin

I'm using Pandas to explore some datasets. I have this dataframe:

我正在使用 Pandas 来探索一些数据集。我有这个数据框:

enter image description here

在此处输入图片说明

I want to exclude any row that has a city value. So I've tried:

我想排除任何具有城市值的行。所以我试过:

new_df = all_df[(all_df["City"] == "None") ]
new_df

But then I got an empty dataframe:

但是后来我得到了一个空的数据框:

enter image description here

在此处输入图片说明

It works whenever I use any value other than None. Any idea how to filter this dataframe?

每当我使用除None. 知道如何过滤这个数据框吗?

采纳答案by tarashypka

Consider using isnull()to locate missing values

考虑使用isnull()定位缺失值

all_df[all_df['City'].isnull()]

回答by Bodhi94

I hope "where" can do what you expect

我希望“ where”能做到你所期望的

new_df = new_df.where(new_df["city"], None) 

And it is better use np.nanrather than None.

它更好地使用np.nan而不是None.

For more details pandas.DataFrame.where

有关更多详细信息pandas.DataFrame.where

回答by imanzabet

Try this to select only the Nonevalues for city column:

试试这个只选择None城市列的值:

new_df = all_df['City'][all_df['City'] == "None"]

Try this to see all other columns which has the same rows of 'City'==None

试试这个以查看具有相同行的所有其他列 'City'==None

new_df = all_df[all_df['City'] == "None"]
print(new_df.head()) # with function head() you can see the first 5 rows