Pandas - Filtering None Values
88,160
Solution 1
Consider using isnull()
to locate missing values
all_df[all_df['City'].isnull()]
Solution 2
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
Author by
Shadin
Updated on July 05, 2022Comments
-
Shadin almost 2 years
I'm using Pandas to explore some datasets. I have this dataframe:
I want to exclude any row that has a city value. So I've tried:
new_df = all_df[(all_df["City"] == "None") ] new_df
But then I got an empty dataframe:
It works whenever I use any value other than
None
. Any idea how to filter this dataframe? -
Shadin almost 7 yearsThanks! but when i print the new df i get: Series([], Name: City, dtype: object)
-
Shadin almost 7 yearsHow can i print it properly?
-
imanzabet almost 7 yearsOk, I think I got your question. You need to print all other columns as well for city==None. I have edited the codes. Hope it will be helpful
-
fstang about 5 yearsTo exclude those rows: all_df[~all_df['City'].isnull()]
-
tarashypka about 5 years@fstang You can also use notnull:
all_df[all_df['City'].notnull()]
-
Nabin about 4 yearsHow about getting all that is not None?
-
BND almost 3 years@Nabin
all_df[~all_df['City'].isnull()]
-
Olsgaard over 2 years
isnull()
returns True for bothNone
andNaN
values.