pandas 保留 NaN 值并删除非缺失值

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

Keeping NaN values and dropping nonmissing values

pythonpandas

提问by ctan

I have a DataFrame where I would like to keep the rows when a particular variable has a NaN value and drop the nonmissing values.

我有一个 DataFrame,当特定变量具有 NaN 值并删除非缺失值时,我想在其中保留行。

Example:

例子:

    ticker  opinion  x1       x2  
    aapl    GC       100      70  
    msft    NaN      50       40  
    goog    GC       40       60  
    wmt     GC       45       15  
    abm     NaN      80       90  

In the above DataFrame, I would like to drop all observations where opinion is not missing (so, I would like to drop the rows where ticker is aapl, goog, and wmt).

在上面的 DataFrame 中,我想删除所有没有缺少意见的观察(因此,我想删除股票代码为 aapl、goog 和 wmt 的行)。

Is there anything in pandas that is the opposite to .dropna()?

Pandas中有什么与.dropna()?

回答by Roger Fan

Use pandas.isnullon the column to find the missing values and index with the result.

pandas.isnull在列上使用以查找缺失值和结果索引。

import pandas as pd

data = pd.DataFrame({'ticker': ['aapl', 'msft', 'goog'],
                     'opinion': ['GC', nan, 'GC'],
                     'x1': [100, 50, 40]})

data = data[pd.isnull(data['opinion'])]