Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

index of non "NaN" values in Pandas

Tags:

python

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 
like image 599
d.putto Avatar asked Jul 01 '14 13:07

d.putto


People also ask

Is not NaN in pandas?

isna() in pandas library can be used to check if the value is null/NaN. It will return True if the value is NaN/null.

How do you check if a value is not NaN in Python?

The math. isnan() method checks whether a value is NaN (Not a Number), or not. This method returns True if the specified value is a NaN, otherwise it returns False.

How do you filter out NaN values pandas?

You can filter out rows with NAN value from pandas DataFrame column string, float, datetime e.t.c by using DataFrame. dropna() and DataFrame. notnull() methods. Python doesn't support Null hence any missing data is represented as None or NaN.


2 Answers

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 
like image 84
EdChum Avatar answered Sep 21 '22 20:09

EdChum


DataFrames have a dropna method:

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']) 
like image 23
chthonicdaemon Avatar answered Sep 22 '22 20:09

chthonicdaemon