pandas load data with data type issues
<p>Here is the code, output and raw csv file data, the dtypes are all object type from output, is there a way to recognize each column as string (and last column as float type)? Using Python 2.7 with miniconda.</p>

<p>Code,</p>

<pre><code>import pandas as pd
sample=pd.read_csv('123.csv', sep=',',header=None)
print sample.dtypes
</code></pre>

<p>program output,</p>

<pre><code>0    object
1    object
2    object
3    object
</code></pre>

<p>123.csv content,</p>

<pre><code>c_a,c_b,c_c,c_d
hello,python,pandas,1.2
</code></pre>

<p><strong>Edit 1</strong>,</p>

<pre><code>sample = pd.read_csv('123.csv', header=None, skiprows=1,
    dtype={0:str, 1:str, 2:str, 3:str})
print sample.dtypes

0    object
1    object
2    object
3    object
dtype: object
</code></pre>

<p><strong>Edit 2</strong>,</p>

<pre><code>sample = pd.read_csv('123.csv', header=None, skiprows=1,
    dtype={0:str, 1:str, 2:str, 3:str})
sample.columns = pd.Index(data=['c_a', 'c_b', 'c_c', 'c_d'])
sample['c_d'] = sample['c_d'].astype('float32')
print sample.dtypes

c_a     object
c_b     object
c_c     object
c_d    float32
</code></pre>

<p>regards,
Lin</p>

<p>You have to use the argument <code>dtype</code>. And since you do not want the header, you must skip it with <code>skiprows</code> because the third element is not a float.</p>

<pre><code>df = pd.read_csv('123.csv', header=None, skiprows=1,
            dtype={0:str, 1:str, 2:str, 3:float})
</code></pre>

<p>The output is:</p>

<pre><code>       0       1       2    3
0  hello  python  pandas  1.2
</code></pre>

<p><strong>EDIT:</strong></p>

<p>To add a header with different types to your DataFrame, you can use:</p>

<pre><code>df.columns = pd.Index(data=['c_a', 'c_b', 'c_d', 4.])
</code></pre>

<p>and the output is:</p>

<pre><code>     c_a     c_b     c_d  4.0
0  hello  python  pandas  1.2
</code></pre>

python python-2.7 pandas