非"NaN"指数 pandas 的价值观 [英] index of non "NaN" values in Pandas

查看:55
本文介绍了非"NaN"指数 pandas 的价值观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从熊猫数据框中获取非"NaN"值的索引?

From Pandas data frame, how to get index of non "NaN" values?

我的数据框是

    A    b     c
0   1    q1    1
1   2    NaN   3
2   3    q2    3
3   4    q1    NaN
4   5    q2    7

我想要 b 列不是NaN的行的索引. (在其他列(例如c)中可能有NaN值)

And I want the index of the rows in which column b is not NaN. (there can be NaN values in other column e.g. c )

non_nana_index = [0,2,3,4]

non_nana_index = [0,2,3,4]

使用此非"NaN"索引列表,我要创建一个新数据框,其中 b 列不包含"Nan"

Using this non "NaN" index list I want to create new data frame which column b do not have "Nan"

df2 =

    A    b     c
0   1    q1    1
1   3    q2    3
2   4    q1    NaN
3   5    q2    7

推荐答案

只需过滤它们

In [62]:

df['b'].notnull()

Out[62]:
0     True
1    False
2     True
3     True
4     True
Name: b, dtype: bool
In [63]:

df[df['b'].notnull()]
Out[63]:
   A   b   c
0  1  q1   1
2  3  q2   3
3  4  q1 NaN
4  5  q2   7

这篇关于非"NaN"指数 pandas 的价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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