Pandas - dropping rows with missing data not working using .isnull(), notnull(), dropna()
Your example DF has NaN
and NaT
as strings which .dropna
, .notnull
and co. won't consider falsey, so given your example you can use...
df[~df.isin(['NaN', 'NaT']).any(axis=1)]
Which gives you:
A B C
0 1 1 1
1 2 2 2
4 5 5 5
If you had a DF such as (note of the use of np.nan
and np.datetime64('NaT')
instead of strings:
df = pd.DataFrame({'A':[1,2,3,4,5],'B':[1,2,np.nan,4,5],'C':[1,2,3,np.datetime64('NaT'),5]})
Then running df.dropna()
which give you:
A B C
0 1 1.0 1
1 2 2.0 2
4 5 5.0 5
Note that column B
is now a float
instead of an integer as that's required to store NaN
values.
Try this on orig data:
Test.replace(["NaN", 'NaT'], np.nan, inplace = True)
Test = Test.dropna()
Test
Or Modify data and do this
import pandas as pd
import numpy as np
Test = pd.DataFrame({'A':[1,2,3,4,5],'B':[1,2,np.nan,4,5],'C':[1,2,3,pd.NaT,5]})
print(Test)
Test = Test.dropna()
print(Test)
A B C
0 1 1.0 1
1 2 2.0 2
4 5 5.0 5