pandas.DataFrame set all string values to nan
<p>I have a <code>pandas.DataFrame</code> that contain string, float and int types.</p>

<p>Is there a way to set all strings that cannot be converted to float to <code>NaN</code> ?</p>

<p>For example:</p>

<pre class="lang-py prettyprint-override"><code>    A  B   C      D
0   1  2   5      7
1   0  4 NaN     15
2   4  8   9     10
3  11  5   8      0
4  11  5   8  "wajdi"
</code></pre>

<p>to:</p>

<pre class="lang-py prettyprint-override"><code>    A  B   C      D
0   1  2   5      7
1   0  4 NaN     15
2   4  8   9     10
3  11  5   8      0
4  11  5   8    NaN
</code></pre>

<p>Try </p>

<pre><code>df.convert_objects(convert_numeric=True)
</code></pre>

<p>To be fair to the other answers using  to_numeric, this method is deprecated as Merlin pointed out.  So make sure to take that into account when using this, especially if you plan to roll it into a long term solution. Although, you should be fine if you're just applying this to an ad-hoc analysis. </p>

<p><a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.convert_objects.html" rel="nofollow">Pandas.DataFrame.convert_objects</a></p>

python string pandas dataframe