保留 NaN 值并删除非缺失值

编程入门 行业动态 更新时间:2024-10-24 13:30:38
本文介绍了保留 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 值并删除非缺失值

本文发布于:2023-10-14 12:02:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1491009.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缺失   NaN

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!