Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Fill NaN with zeros in a Pandas Dataframe

Tags:

python

pandas

nan

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.

like image 858
prre72 Avatar asked Dec 08 '22 09:12

prre72


1 Answers

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
like image 68
unutbu Avatar answered Dec 11 '22 11:12

unutbu