df.fillna(0) command won't replace NaN values with 0

后端 未结 1 1987
自闭症患者
自闭症患者 2021-01-14 15:12

I\'m trying to replace the NaN values generated in the code below to 0. I don\'t understand what the below won\'t work. It still keeps the NaN values.

df_pub         


        
1条回答
  •  不知归路
    2021-01-14 16:03

    You need to assign the result of fillna:

    df_pubs = df_pubs.fillna(0)
    

    or pass param inplace=True:

    df_pubs.fillna(0, inplace=True)
    

    See the docs

    You could modify your code to this:

    df_pubs = df_pubs.pivot(index='Conference', columns='Year', values='totalPubs').fillna(0)
    

    which would work but it's debatable whether the fillna is readable here.

    0 讨论(0)
提交回复
热议问题