Python Pandas 中非“NaN”值的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24511200/
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
index of non "NaN" values in Pandas
提问by d.putto
From Pandas data frame, how to get index of non "NaN" values?
从 Pandas 数据框中,如何获取非“NaN”值的索引?
My data frame is
我的数据框是
A b c
0 1 q1 1
1 2 NaN 3
2 3 q2 3
3 4 q1 NaN
4 5 q2 7
And I want the index of the rows in which column bis not NaN. (there can be NaN values in other column e.g. c )
我想要列b不是 NaN的行的索引。(其他列中可以有 NaN 值,例如 c )
non_nana_index = [0,2,3,4]
non_nana_index = [0,2,3,4]
Using this non "NaN" index list I want to create new data frame which column bdo not have "Nan"
使用这个非“NaN”索引列表,我想创建新的数据框,其中b列没有“Nan”
df2=
df2=
A b c
0 1 q1 1
1 3 q2 3
2 4 q1 NaN
3 5 q2 7
采纳答案by EdChum
Just filter them
只需过滤它们
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
回答by chthonicdaemon
DataFrames have a dropna
method:
DataFrames 有一个dropna
方法:
import pandas
import numpy
d = pandas.DataFrame({'A': [1, 2, 3, numpy.nan],
'b': [1, 2, numpy.nan, 3],
'c': [1, numpy.nan, 2, 3]})
d.dropna(subset=['b'])