Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas dataframe fillna() only some columns in place

I am trying to fill none values in a Pandas dataframe with 0's for only some subset of columns.

When I do:

import pandas as pd df = pd.DataFrame(data={'a':[1,2,3,None],'b':[4,5,None,6],'c':[None,None,7,8]}) print df df.fillna(value=0, inplace=True) print df 

The output:

     a    b    c 0  1.0  4.0  NaN 1  2.0  5.0  NaN 2  3.0  NaN  7.0 3  NaN  6.0  8.0      a    b    c 0  1.0  4.0  0.0 1  2.0  5.0  0.0 2  3.0  0.0  7.0 3  0.0  6.0  8.0 

It replaces every None with 0's. What I want to do is, only replace Nones in columns a and b, but not c.

What is the best way of doing this?

like image 265
Sait Avatar asked Jun 30 '16 22:06

Sait


People also ask

Is Fillna an inplace?

The 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.

How do I fill NaN values in multiple columns?

We can use fillna() function to impute the missing values of a data frame to every column defined by a dictionary of values. The limitation of this method is that we can only use constant values to be filled.

How do I fill NA values in a column?

fillna() method is used to fill NaN/NA values on a specified column or on an entire DataaFrame with any given value. You can specify modify using inplace, or limit how many filling to perform or choose an axis whether to fill on rows/column etc. The Below example fills all NaN values with None value.


2 Answers

You can select your desired columns and do it by assignment:

df[['a', 'b']] = df[['a','b']].fillna(value=0) 

The resulting output is as expected:

     a    b    c 0  1.0  4.0  NaN 1  2.0  5.0  NaN 2  3.0  0.0  7.0 3  0.0  6.0  8.0 
like image 186
root Avatar answered Oct 11 '22 20:10

root


You can using dict , fillna with different value for different column

df.fillna({'a':0,'b':0}) Out[829]:       a    b    c 0  1.0  4.0  NaN 1  2.0  5.0  NaN 2  3.0  0.0  7.0 3  0.0  6.0  8.0 

After assign it back

df=df.fillna({'a':0,'b':0}) df Out[831]:       a    b    c 0  1.0  4.0  NaN 1  2.0  5.0  NaN 2  3.0  0.0  7.0 3  0.0  6.0  8.0 
like image 28
BENY Avatar answered Oct 11 '22 21:10

BENY