How to convert non-numeric entries to NaN in read_csv
<p>I am reading in a file with</p>

<pre><code>pd.read_csv("file.csv", dtype={'ID_1':float})
</code></pre>

<p>The file looks like</p>

<pre><code>ID_0, ID_1,ID_2
a,002,c
b,004,d
c,   ,e       
n,003,g
</code></pre>

<p>Unfortunately read_csv fails complaining it can't convert '  ' to a float.</p>

<p>What is the right way to read in a csv and convert anything that can't be converted to a float into NaN?</p>

<p>If you don't specify the <code>dtype</code> param and pass <code>skipinitialspace=True</code> then it will just work:</p>

<pre><code>In [4]:
t="""ID_0,ID_1,ID_2
a,002,c
b,004,d
c,   ,e
n,003,g"""

pd.read_csv(io.StringIO(t), skipinitialspace=True)
Out[4]:
  ID_0  ID_1 ID_2
0    a   2.0    c
1    b   4.0    d
2    c   NaN    e
3    n   3.0    g
</code></pre>

<p>So in your case:</p>

<pre><code>pd.read_csv("file.csv", skipinitialspace=True)
</code></pre>

<p>will just work</p>

<p>You can see that the <code>dtypes</code> are as expected:</p>

<pre><code>In [5]:
pd.read_csv(io.StringIO(t), skipinitialspace=True).info()

&lt;class 'pandas.core.frame.DataFrame'&gt;
RangeIndex: 4 entries, 0 to 3
Data columns (total 3 columns):
ID_0    4 non-null object
ID_1    3 non-null float64
ID_2    4 non-null object
dtypes: float64(1), object(2)
memory usage: 176.0+ bytes
</code></pre>

python pandas