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>This is my understanding of reading the documentation:</p>

<pre><code>def my_func(x):
    try:
        converted_value = float(x)
    except ValueError:
        converted_value = 'NaN'
    return converted_value

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

<p>As i am at work now and don't have access to <code>pandas</code> i cannot tell you if it works but it looks as it should (said every programmer ever..)</p>

<p>Also you might want to take a look at these relevant SO questions:</p>

<p><a href="http://stackoverflow.com/questions/18471859/pandas-read-csv-dtype-inference-issue">pandas read_csv dtype inference issue</a></p>

<p><a href="http://stackoverflow.com/questions/25669588/convert-percent-string-to-float-in-pandas-read-csv">Convert percent string to float in pandas read_csv</a></p>

<p>Finally, the <code>pandas.read_csv</code> documentation is <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html#pandas-read-csv" rel="nofollow">here</a></p>

python pandas