1

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.

2
  • 'None' should be None. Commented Aug 3, 2020 at 12:33
  • df where the col2 only includes number 2,8 and None...What if there's a NaN in col2 should output df include NaN or not? Commented Aug 3, 2020 at 12:46

2 Answers 2

3

if you want to include any null value:

filter = df['col2'].isin([2,8]) | df['col2'].isnull() df_filtered = df.loc[filter] 
Sign up to request clarification or add additional context in comments.

Comments

2

Exclude the '' when passing None in your filter

filter1 = [2,8,None] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.