保留 NaN 值并删除非缺失值 [英] Keeping NaN values and dropping nonmissing values

查看:60
本文介绍了保留 NaN 值并删除非缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

示例:

代码意见 x1 x2aapl GC 100 70msft NaN 50 40谷歌 GC 40 60wmt GC 45 15abm NaN 80 90

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

pandas 中是否有与 .dropna() 相反的东西?

解决方案

使用 pandas.Series.isnull 在列上查找缺失值并用结果索引.

将pandas导入为pddata = pd.DataFrame({'ticker': ['aapl', 'msft', 'goog'],'意见': ['GC', nan, 'GC'],'x1': [100, 50, 40]})数据 = 数据[数据['意见'].isnull()]

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

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).

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

解决方案

Use pandas.Series.isnull on the column to find the missing values and index with the result.

import pandas as pd

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

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

这篇关于保留 NaN 值并删除非缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆