Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

python - index of non "NaN" values in Pandas

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

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 b is not NaN. (there can be NaN values in other column e.g. c )

non_nana_index = [0,2,3,4]

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...