Convert NaN string to null

2,304 views
Skip to first unread message

Paul Blelloch

unread,
Nov 29, 2014, 9:43:01 PM11/29/14
to pyd...@googlegroups.com
I'm probably missing something obvious here, but I'm creating a dataframe from a duct that contains a number of 'NaN' strings for missing data. Pandas seems to be interpreting these literally as strings. How do I convert them to nulls so that I can use methods such as dropna to deal with them?

Phillip Cloud

unread,
Nov 29, 2014, 9:54:41 PM11/29/14
to pyd...@googlegroups.com

One option is to use the replace method :

In [10]: import pandas as pd

In [11]: df
Out[11]:
     a  b
0  NaN  1
1    1  2
2  NaN  3
3    1  1
4  NaN  2
5    1  3

In [12]: df.dtypes
Out[12]:
a    object
b     int64
dtype: object

In [13]: newdf = df.replace('NaN', np.nan)

In [14]: newdf
Out[14]:
    a  b
0 NaN  1
1   1  2
2 NaN  3
3   1  1
4 NaN  2
5   1  3

In [15]: newdf.dtypes
Out[15]:
a    float64
b      int64
dtype: object


--
Best,
Phillip Cloud

On Sat, Nov 29, 2014 at 9:43 PM, Paul Blelloch <paul.b...@gmail.com> wrote:
I'm probably missing something obvious here, but I'm creating a dataframe from a duct that contains a number of 'NaN' strings for missing data. Pandas seems to be interpreting these literally as strings. How do I convert them to nulls so that I can use methods such as dropna to deal with them?

--
You received this message because you are subscribed to the Google Groups "PyData" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydata+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paul Blelloch

unread,
Nov 30, 2014, 12:53:29 AM11/30/14
to pyd...@googlegroups.com
Thanks. That's exactly what I was looking for. I had been missing the np.nan.
Reply all
Reply to author
Forward
0 new messages