Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas fillna not working

Tags:

python

pandas

I have a dataframe with nans in it:

>>>df.head() Out[1]:              JPM US SMALLER COMPANIES C ACC 1990-01-02                             NaN 1990-01-03                             NaN 1990-01-04                             NaN 1990-01-05                             NaN 1990-01-08                             NaN 

I have another dataframe with values in it:

>>>t.head() Out[1]:  1990-01-02    51.95 1990-01-03    52.63 1990-01-04    53.04 1990-01-05    52.07 1990-01-08    51.73 Name: JPM US SMALLER COMPANIES C ACC, dtype: float64 

Unfortunately, df.fillna does not appear to be working for me:

>>>df.fillna( t ).head() Out[1]:              JPM US SMALLER COMPANIES C ACC 1990-01-02                             NaN 1990-01-03                             NaN 1990-01-04                             NaN 1990-01-05                             NaN 1990-01-08                             NaN  [5 rows x 1 columns] 

Why is this happening? I am on pandas 0.13.1

like image 469
Ginger Avatar asked Aug 05 '14 20:08

Ginger


People also ask

How do I use Fillna in pandas?

Definition and UsageThe fillna() method replaces the NULL values with a specified value. The fillna() method returns a new DataFrame object unless the inplace parameter is set to True , in that case the fillna() method does the replacing in the original DataFrame instead.

Does Fillna work on NaN?

The fillna() function is used to fill NA/NaN values using the specified method. Value to use to fill holes (e.g. 0), alternately a dict/Series/DataFrame of values specifying which value to use for each index (for a Series) or column (for a DataFrame). Values not in the dict/Series/DataFrame will not be filled.


2 Answers

You need inplace=True

df[1].fillna(0, inplace=True) 
like image 187
zinking Avatar answered Sep 22 '22 13:09

zinking


You need to assign the value df = df.fillna( t )

like image 24
Ravikant Singh Avatar answered Sep 18 '22 13:09

Ravikant Singh