Search code examples
pythonpandasfilterisin

Including None in dataframe filter


I have a dataframe called df:

     col1    col2
      1       2
      3       3
      7       8
      5       2
     12       8
      5       None

I want to use pandas.series.isin to filter the data frame, where I want to filter the data from df where the col2 only includes number 2,8 and None, so the new data frame is:

     col1    col2
      1       2
      7       8
      5       2
     12       8
      5       None

I tried:

filter1 = [2,8,'None']
filter2 = [2,8,np.nan]
filter3 = [2,8,'']

df.col2.isin(filter1)
df.col2.isin(filter2)
df.col2.isin(filter3)

And the result is always:

     col1    col2
      1       2
      7       8
      5       2
     12       8

it seems I am unable to identify what is None and how to include it into the filter.


Solution

  • Exclude the '' when passing None in your filter

    filter1 = [2,8,None]