Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas replace null values for a subset of columns

Tags:

python

pandas

I have a data frame with many columns, say:

df:
name   salary  age   title
John      100   35    eng
Bill      200  NaN    adm
Lena      NaN   28    NaN
Jane      120   45    eng

I want to replace the null values in salary and age, but no in the other columns. I know I can do something like this:

u = df[['salary', 'age']]
df[['salary', 'age']] = u.fillna(-1)

But this seems terse as it involves copying. Is there a more efficient way to do this?

like image 468
breezymri Avatar asked May 12 '16 15:05

breezymri


2 Answers

I was hoping fillna() had subset parameter like drop(), maybe should post request to pandas however this is the cleanest version in my opinion.

df[["salary", "age"]] = df[["salary", "age"]].fillna(-1)
like image 191
haneulkim Avatar answered Oct 17 '22 16:10

haneulkim


According to Pandas documentation in 23.3

values = {'salary': -1, 'age': -1}
df.fillna(value=values, inplace=True)
like image 41
Muhammad Raihan Muhaimin Avatar answered Oct 17 '22 15:10

Muhammad Raihan Muhaimin