pd.read_csv by default treats integers like floats
<p>I have a <code>csv</code> that looks like (headers = first row):</p>

<pre><code>name,a,a1,b,b1
arnold,300311,arnld01,300311,arnld01
sam,300713,sam01,300713,sam01
</code></pre>

<p>When I run:</p>

<pre><code>df = pd.read_csv('file.csv')
</code></pre>

<p>Columns <code>a</code> and <code>b</code> have a <code>.0</code> attached to the end like so:</p>

<pre><code>df.head()

name,a,a1,b,b1
arnold,300311.0,arnld01,300311.0,arnld01
sam,300713.0,sam01,300713.0,sam01
</code></pre>

<p>Columns <code>a</code> and <code>b</code> are integers or blanks so why does <code>pd.read_csv()</code> treat them like floats and how do I ensure they are integers on the read? </p>

<p>As <a href="http://stackoverflow.com/questions/39666308/pd-read-csv-by-default-treats-integers-like-floats#comment66634324_39666308">root</a> mentioned in the comments, this is a limitation of Pandas (and Numpy). <code>NaN</code> is a float and the empty values you have in your CSV are NaN.</p>

<p>This is listed in the <a href="http://pandas.pydata.org/pandas-docs/stable/gotchas.html#na-type-promotions" rel="nofollow">gotchas</a> of pandas as well. </p>

<p>You can work around this in a few ways. </p>

<p>For the examples below I used the following to import the data - note that I added a row with an empty value in columns <code>a</code> and <code>b</code></p>

<pre><code>import pandas as pd
from StringIO import StringIO

data = """name,a,a1,b,b1
arnold,300311,arnld01,300311,arnld01
sam,300713,sam01,300713,sam01
test,,test01,,test01"""

df = pd.read_csv(StringIO(data), sep=",")
</code></pre>

<h3>Drop NaN rows</h3>

<p>Your first option is to drop rows that contain this <code>NaN</code> value. The downside of this, is that you lose the entire row. After getting your data into a dataframe, run this:</p>

<pre><code>df.dropna(inplace=True)
df.a = df.a.astype(int)
df.b = df.b.astype(int)
</code></pre>

<p>This drops all <code>NaN</code> rows from the dataframe, then it converts column <code>a</code> and column <code>b</code> to an <code>int</code></p>

<pre><code>&gt;&gt;&gt; df.dtypes
name    object
a        int32
a1      object
b        int32
b1      object
dtype: object

&gt;&gt;&gt; df
     name       a       a1       b       b1
0  arnold  300311  arnld01  300311  arnld01
1     sam  300713    sam01  300713    sam01
</code></pre>

<h3>Fill <code>NaN</code> with placeholder data</h3>

<p>This option will replace all your <code>NaN</code> values with a throw away value. That value is something you need to determine. For this test, I made it <code>-999999</code>. This will allow use to keep the rest of the data, convert it to an int, and make it obvious what data is invalid. You'll be able to filter these rows out if you are making calculations based on the columns later. </p>

<pre><code>df.fillna(-999999, inplace=True)
df.a = df.a.astype(int)
df.b = df.b.astype(int)
</code></pre>

<p>This produces a dataframe like so:</p>

<pre><code>&gt;&gt;&gt; df.dtypes
name    object
a        int32
a1      object
b        int32
b1      object
dtype: object

&gt;&gt;&gt; df
     name       a       a1       b       b1
0  arnold  300311  arnld01  300311  arnld01
1     sam  300713    sam01  300713    sam01
2    test -999999   test01 -999999   test01
</code></pre>

<h3>Leave the float values</h3>

<p>Finally, another choice is to leave the float values (and <code>NaN</code>) and not worry about the non-integer data type. </p>

python csv pandas integer