How to replace Values in Data Frame

3,293 views
Skip to first unread message

erik...@hotmail.com

unread,
Nov 9, 2011, 5:12:52 AM11/9/11
to pystatsmodels
Hi

i have a df like this, and going to perlace the missing values -9999
to NaN or whatever
how can i do it easy way?

Thank's a lot;-)

given:

In [215]: df1
Out[215]:
data1 data2
2005-01-31 -9999 -9999
2005-02-28 -9999 -9999
2005-03-31 2 22
2005-04-30 3 21
2005-05-31 4 20
2005-06-30 5 19
2005-07-31 6 18
2005-08-31 7 17
2005-09-30 8 16
2005-10-31 9 15
2005-11-30 10 14
2005-12-31 11 13

desired result:
In [215]: df1
Out[215]:
data1 data2
2005-01-31 NaN NaN
2005-02-28 NaN NaN
2005-03-31 2 22
2005-04-30 3 21
2005-05-31 4 20
2005-06-30 5 19
2005-07-31 6 18
2005-08-31 7 17
2005-09-30 8 16
2005-10-31 9 15
2005-11-30 10 14
2005-12-31 11 13



Adam Klein

unread,
Nov 9, 2011, 10:58:23 AM11/9/11
to pystat...@googlegroups.com
Hey Erik, 

You can always just loop through:

for col in df1:
    for i, val in enumerate(col):
        if df[col][i] == -9999:
            df[col][i] = numpy.NaN

Adam Klein

unread,
Nov 9, 2011, 11:01:57 AM11/9/11
to pystat...@googlegroups.com
Of course, as soon as I wrote it I realized there's a better way:

for k, v in df1.iteritems():
    v[v == -9999] = numpy.NaN

Wes McKinney

unread,
Nov 9, 2011, 11:09:46 AM11/9/11
to pystat...@googlegroups.com
df[df == -9999] = np.nan 

should work if the DataFrame is homogeneous (all columns floating point)

erik...@hotmail.com

unread,
Nov 9, 2011, 11:17:54 AM11/9/11
to pystatsmodels
Thanks for the super quick responses, pandas is just great:-)

On 9 Nov., 17:09, Wes McKinney <wesmck...@gmail.com> wrote:
> On Nov 9, 2011, at 11:01 AM, Adam Klein <a...@lambdafoundry.com> wrote:
>
>
>
>
>
>
>
>
>
> On Wed, Nov 9, 2011 at 10:58 AM, Adam Klein <a...@lambdafoundry.com> wrote:
Reply all
Reply to author
Forward
0 new messages