Cannot Fill NaN with zeros in a Pandas Dataframe

风格不统一 提交于 2019-12-07 17:55:31

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/21801507/cannot-fill-nan-with-zeros-in-a-pandas-dataframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!