Replacing the missing values in pandas

落花浮王杯 提交于 2021-02-02 09:58:49

问题


I have a pandas dataframe where missing values are indicated as -999.

In [58]: df.head()
Out[58]: 

EventId                    A                  B                    C
100000                   0.91           124.711             2.666000   
100001                -999.00          -999.000            -0.202838   
100002                -999.00          -999.000            -0.202838   
100003                -999.00          -999.000            -0.202838   

I want to replace the missing values (indicated by -999) with the mean of that column taken over non-missing values. Which is the best way to do this? Is there any pandas function which can be used to achieve this easily?


回答1:


df2.replace(-999, np.nan, inplace=True)
df2.fillna(df2.mean())

    EventId A       B        C
0   100000  0.91    124.711  2.666000
1   100001  0.91    124.711 -0.202838
2   100002  0.91    124.711 -0.202838
3   100003  0.91    124.711 -0.202838


来源:https://stackoverflow.com/questions/33637477/replacing-the-missing-values-in-pandas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!