prre72
prre72

Reputation: 717

Cannot Fill NaN with zeros in a Pandas Dataframe

I have the following problem: I am reading a csv file with missing values by using

pd.read_csv(f_name, sep=sep, header=hdr, parse_dates=True, index_col=date_col, quotechar=quote)

The dataframe I get has 'nan's in it (I was expecting 'NaN's with the Upper cases). Now if I try to replace those nan's with zerosby using

df.fillna(0)

my df doesn't change (I still see nan's in it) My guess is that fillna is not working because I have nan (lowercase) instead of NaN (uppercase). Am I correct? If yes, do you have an idea why pd.read.csv returns a dataframe with lowercase nan's? I am using Python 2.7.6 (Anaconda bundle)

Many thanks for your help.

Upvotes: 4

Views: 7674

Answers (1)

unutbu
unutbu

Reputation: 880927

df.fillna(0) returns a new dataframe; it does not alter df.

So instead use:

df = df.fillna(0)           # assigns df to a new dataframe

Upvotes: 14

Related Questions