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
Share:
88,160
Shadin
Author by

Shadin

Updated on July 05, 2022

Comments

  • Shadin
    Shadin almost 2 years

    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 city value. 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?

  • Shadin
    Shadin almost 7 years
    Thanks! but when i print the new df i get: Series([], Name: City, dtype: object)
  • Shadin
    Shadin almost 7 years
    How can i print it properly?
  • imanzabet
    imanzabet almost 7 years
    Ok, 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
    fstang about 5 years
    To exclude those rows: all_df[~all_df['City'].isnull()]
  • tarashypka
    tarashypka about 5 years
    @fstang You can also use notnull: all_df[all_df['City'].notnull()]
  • Nabin
    Nabin about 4 years
    How about getting all that is not None?
  • BND
    BND almost 3 years
    @Nabin all_df[~all_df['City'].isnull()]
  • Olsgaard
    Olsgaard over 2 years
    isnull() returns True for both None and NaN values.