16

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?

breezymri
  • 3,975
  • 8
  • 31
  • 65

5 Answers5

25

According to Pandas documentation in 23.3

values = {'salary': -1, 'age': -1}
df.fillna(value=values, inplace=True)
easf
  • 25
  • 5
Muhammad Raihan Muhaimin
  • 5,559
  • 7
  • 47
  • 68
16

Try this:

subset = ['salary', 'age']
df.loc[:, subset] = df.loc[:, subset].fillna(-1)
craymichael
  • 4,578
  • 1
  • 15
  • 24
piRSquared
  • 285,575
  • 57
  • 475
  • 624
7

It is not so beautiful, but it works:

df.salary.fillna(-1, inplace=True)
df.age.fillna(-1, inplace=True)
df
>>>    name  salary   age title
    0  John   101.0  35.0   eng
    1  Bill   200.0  -1.0   adm
    2  Lena    -1.0  28.0   NaN
    3  Jane   120.0  45.0   eng
xirururu
  • 5,028
  • 9
  • 35
  • 64
5

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)
haneulkim
  • 4,406
  • 9
  • 38
  • 80
2

You can do:

df = df.assign(
    salary=df.salary.fillna(-1),
    age=df.age.fillna(-1),
)

if you want to chain it with other operations.

kris
  • 23,024
  • 10
  • 70
  • 79