Pandas DataFrame 选择具有 NaN 值的特定列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38368490/
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
Pandas DataFrame select the specific columns with NaN values
提问by Fan
I have a two-column DataFrame, I want to select the rows with NaN
in either column.
我有一个两列的 DataFrame,我想选择NaN
任一列中的行。
I used this method df[ (df['a'] == np.NaN) | (df['b'] == np.NaN) ]
However it returns an empty answer. I don't know what's the problem
我使用了这个方法df[ (df['a'] == np.NaN) | (df['b'] == np.NaN) ]
但是它返回一个空答案。我不知道有什么问题
采纳答案by jezrael
回答by piRSquared
You could apply isnull()
to the whole dataframe then check if the rows have any nulls with any(1)
您可以应用isnull()
到整个数据框然后检查行是否有任何空值any(1)
df[df.isnull().any(1)]
Timing
定时
df = pd.DataFrame(np.random.choice((np.nan, 1), (1000000, 2), p=(.2, .8)), columns=['A', 'B'])
I did not expect these results...
没想到这个结果...