Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 views
in Technique[技术] by (71.8m points)

dataframe - Replacing the missing values in pandas

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...