Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - Filtering None Values

I'm using Pandas to explore some datasets. I have this dataframe:

enter image description here

I want to exclude any row that has a value in column City. So I've tried:

new_df = all_df[(all_df["City"] == "None") ]
new_df

But then I got an empty dataframe:

enter image description here

It works whenever I use any value other than None. Any idea how to filter this dataframe?

like image 495
Shadin Avatar asked Jul 15 '17 10:07

Shadin


3 Answers

Consider using isnull() to locate missing values

all_df[all_df['City'].isnull()]
like image 59
tarashypka Avatar answered Oct 17 '22 01:10

tarashypka


Try this to select only the None values for city column:

new_df = all_df['City'][all_df['City'] == "None"]

Try this to see all other columns which has the same rows of 'City'==None

new_df = all_df[all_df['City'] == "None"]
print(new_df.head()) # with function head() you can see the first 5 rows
like image 31
imanzabet Avatar answered Oct 16 '22 23:10

imanzabet


Another alternative is to use the query method:

In [3]: all_df.query('City != City')
Out[3]: 
   FACTS_Value    Region  City Village
0      34135.0  Al Bahah  None    None
like image 1
rachwa Avatar answered Oct 16 '22 23:10

rachwa