Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting pandas cells with None value

I have a column of a pandas dataframe that I got from a database query with blank cells. The blank cells become "None" and I want to check if each of the rows is None:

In [325]: yes_records_sample['name']
Out[325]: 
41055    John J Murphy Professional Building
25260                                   None
41757             Armand Bayou Nature Center
31397                                   None
33104               Hubert Humphrey Building
16891                         Williams Hall
29618                                   None
3770                          Covenant House
39618                                   None
1342       Bhathal Student Services Building
20506                                   None

My understanding per the documentation is that I can check if each row is null with isnull() command http://pandas.pydata.org/pandas-docs/dev/missing_data.html#values-considered-missing

That function, however, is not working for me:

In [332]: isnull(yes_records_sample['name'])

I get the following error:

NameError Traceback (most recent call last)
<ipython-input-332-55873906e7e6> in <module>()
----> 1 isnull(yes_records_sample['name'])
NameError: name 'isnull' is not defined

I also saw that someone just replaced the "None" strings, but neither of these variations on that approach worked for me: Rename "None" value in Pandas

yes_records_sample['name'].replace('None', "--no value--")
yes_records_sample['name'].replace(None, "--no value--")

I was ultimately able to use the fillna function and fill each of those rows with an empty string yes_records_sample.fillna('') as a workaround and then I could check yes_records_sample['name']=='' But I am profoundly confused by how 'None' works and what it means. Is there a way to easily just check if a cell in a dataframe is 'None'?

like image 711
Michael Discenza Avatar asked Nov 12 '14 17:11

Michael Discenza


People also ask

How do you get none values in pandas?

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.

How do pandas deal with none?

None is also considered a missing value In pandas, None is also treated as a missing value. None is a built-in constant in Python. For numeric columns, None is converted to nan when a DataFrame or Series containing None is created, or None is assigned to an element.

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.


1 Answers

Call it like this:

yes_records_sample['name'].isnull()
like image 183
Korem Avatar answered Sep 28 '22 02:09

Korem