DJV
DJV

Reputation: 873

Pandas not recognizing NaN as null

I have a DataFrame, a portion of it which looks like this:

enter image description here

I read in the file using this line of code:

df = pd.read_table(oname,skiprows=1,sep='\t',usecols=(3,4,5),names=['year','month','snow_depth'])

When I call df.isnull(), I get False for each cell when the NaN should come up as True by default, I believe. Does anyone have an idea why this isn't getting picked up on?

EDIT: Results of df.info()

<class 'pandas.core.frame.DataFrame'>
Int64Index: 360 entries, 516 to 875
Data columns (total 3 columns):
year          360 non-null int64
month         360 non-null int64
snow_depth    360 non-null object
dtypes: int64(2), object(1)
memory usage: 11.2+ KB

Upvotes: 3

Views: 14927

Answers (1)

EdChum
EdChum

Reputation: 394459

It looks like your data had 'NaN' values as ' NaN' so you can add as a param to read_table, na_values=[' NaN'] and this will add this to the default list of values to treat as NaN.

Alternatively you can replace them using:

df.replace(' NaN', np.NaN)

Upvotes: 9

Related Questions