Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering all rows with NaT in a column in Dataframe python

I have a df like this:

    a b           c     1 NaT         w     2 2014-02-01  g     3 NaT         x         df=df[df.b=='2014-02-01'] 

will give me

    a  b          c     2 2014-02-01  g 

I want a database of all rows with NaT in column b?

   df=df[df.b==None] #Doesn't work 

I want this:

    a b           c     1 NaT         w     3 NaT         x     
like image 661
Jase Villam Avatar asked May 19 '14 21:05

Jase Villam


People also ask

How do you filter a DataFrame based on column values in Python?

Using query() to Filter by Column Value in pandas DataFrame. query() function is used to filter rows based on column value in pandas. After applying the expression, it returns a new DataFrame. If you wanted to update the existing DataFrame use inplace=True param.

How do you exclude rows with NA in Python?

By using dropna() method you can drop rows with NaN (Not a Number) and None values from pandas DataFrame. Note that by default it returns the copy of the DataFrame after removing rows. If you wanted to remove from the existing DataFrame, you should use inplace=True .


1 Answers

isnull and notnull work with NaT so you can handle them much the same way you handle NaNs:

>>> df     a          b  c 0  1        NaT  w 1  2 2014-02-01  g 2  3        NaT  x  >>> df.dtypes  a             int64 b    datetime64[ns] c            object 

just use isnull to select:

df[df.b.isnull()]     a   b  c 0  1 NaT  w 2  3 NaT  x 
like image 198
Karl D. Avatar answered Sep 21 '22 08:09

Karl D.