pandas replace null values for a subset of columns

后端 未结 3 2018
無奈伤痛
無奈伤痛 2021-02-14 16:07

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             


        
相关标签:
3条回答
  • 2021-02-14 16:36

    According to Pandas documentation in 23.3

    values = {'salary': -1, 'age': -1}
    df = df.fillna(value=values)
    
    0 讨论(0)
  • 2021-02-14 16:47

    Try this:

    df.loc[:, ['salary', 'age']].fillna(-1, inplace=True)
    
    0 讨论(0)
  • 2021-02-14 16:58

    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
    
    0 讨论(0)
提交回复
热议问题