0

Let's say we have:

import pandas as pd
df = pd.DataFrame([[1, 2], [4, None], [None, 7]], dtype=object, columns=['a', 'b'])
print(df['a'])
# 0       1
# 1       4
# 2    None

I have read Python Pandas Dataframe, remove all rows where 'None' is the value in any column and Pandas - Filtering None Values, and I do know that the solution to remove rows with None is to filter the dataframe with df[~df['a'].isnull()], since we have:

df['a'].isnull()
# 0    False
# 1    False
# 2     True

Question: why don't these two pythonic-looking solutions fail?

df[df['a'] != None]  # fails: filters nothing
df[df['a'] is not None]  # fails too
Basj
  • 41,386
  • 99
  • 383
  • 673

1 Answers1

1

I think because in pandas most time is possible change None with NaN both working with special function like Series.isna, isnull for oldier versions.

Your solution working if test element wise:

df = df[df['a'].apply(lambda x: x is not None)]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252