Boolean indexing to retain falsy values as NaN

Viewed 382

Given a dataframe:

                         Data
1                      246804
2                      135272
3                      898.01
4                     3453.33
5                       shine  
6                        add
7                         522
8                         Nan
9                      string
10                      29.11
11                        20  

I would like two new columns Floats and Strings, both having the same length as the original dataframe. Getting the Floats column is easy:

In [176]: pd.to_numeric(df.Data, errors='coerce')
Out[176]: 
1     246804.00
2     135272.00
3        898.01
4       3453.33
5           NaN
6           NaN
7        522.00
8           NaN
9           NaN
10        29.11
11        20.00
Name: Data, dtype: float64

As you can see, non Floats are coerced to NaN, which is exactly what I want.

To get strings, this is what I do:

In [177]: df[df.Data.str.isalpha()]
Out[177]: 
     Data
5   shine
6     add
8     Nan
9  string

But as you can see, it does not retain the non-String values as NaN. I want something like this:

1                       NaN
2                       NaN
3                       NaN
4                       NaN
5                       shine  
6                       add
7                       NaN
8                       Nan (not NaN)
9                       string
10                      NaN
11                      NaN  

How can I get it to do so?

3 Answers
Related