Python,Pandas:只返回那些有缺失值的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30447083/
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
Python, Pandas : Return only those rows which have missing values
提问by
While working in Pandas in Python...
在 Python 中使用 Pandas 时...
I'm working with a dataset that contains some missing values, and I'd like to return a dataframe which contains only those rows which have missing data. Is there a nice way to do this?
我正在处理一个包含一些缺失值的数据集,我想返回一个数据框,其中只包含那些缺少数据的行。有没有很好的方法来做到这一点?
(My current method to do this is an inefficient "look to see what index isn't in the dataframe without the missing values, then make a df out of those indices.")
(我目前执行此操作的方法是一种低效的“在没有缺失值的情况下查看数据框中没有哪些索引,然后从这些索引中创建 df。”)
采纳答案by metersk
You can use any
axis=1
to check for least one True
per row, then filter with boolean indexing:
您可以使用检查每行至少一个,然后使用布尔索引进行过滤:any
axis=1
True
null_data = df[df.isnull().any(axis=1)]
回答by Ikay
If you are looking for a quicker way to find the total number of missing rows in the dataframe, you can use this:
如果您正在寻找一种更快的方法来查找数据框中缺失行的总数,您可以使用以下方法:
sum(df.isnull().values.any(axis=1))
sum(df.isnull().values.any(axis=1))