Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pandas: selecting rows whose column value is null / None / nan [duplicate]

Tags:

python

pandas

How do I select those rows of a DataFrame whose value in a column is none?

I've coded these to np.nan and can't match against this type.

In [1]: import numpy as np  In [2]: import pandas as pd  In [3]: df = pd.DataFrame([[1, 2, 3], [3, 4, None]])  In [4]: df Out[4]:     0  1    2 0  1  2  3.0 1  3  4  NaN  In [5]: df = df.fillna(np.nan)  In [6]: df Out[6]:     0  1    2 0  1  2  3.0 1  3  4  NaN  In [7]: df.iloc[1][2] Out[7]: nan  In [8]: df.iloc[1][2] == np.nan Out[8]: False  In [9]: df[df[2] == None] Out[9]:  Empty DataFrame Columns: [0, 1, 2] Index: [] 
like image 722
zadrozny Avatar asked Oct 25 '16 16:10

zadrozny


People also ask

How do you filter a pandas DataFrame based on NULL values of a column?

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.

How do I find the null value of a row in Python?

In order to check null values in Pandas DataFrame, we use isnull() function this function return dataframe of Boolean values which are True for NaN values. Code #1: Python.


1 Answers

you can use .isna() method:

In [48]: df[df[2].isna()] Out[48]:    0  1   2 1  3  4 NaN 
like image 187
MaxU - stop WAR against UA Avatar answered Sep 19 '22 23:09

MaxU - stop WAR against UA