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>Here is a way:</p>

<pre><code>df['E'] = pd.to_numeric(df.D, errors='coerce')
</code></pre>

<p>And then you have:</p>

<pre><code>
    A  B    C      D     E
0   1  2  5.0      7   7.0
1   0  4  NaN     15  15.0
2   4  8  9.0     10  10.0
3  11  5  8.0      0   0.0
4  11  5  8.0  wajdi   NaN
</code></pre>

python string pandas dataframe