替换 pandas 中缺失的值 [英] Replacing the missing values in pandas

查看:36
本文介绍了替换 pandas 中缺失的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据框,其中缺失值表示为-999.

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   

我想将丢失的值(由-999表示)替换为该列的平均值代替非缺失值.哪个是最好的方法?有任何熊猫函数可以用来轻松实现这一目标吗?

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?

推荐答案

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

这篇关于替换 pandas 中缺失的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆