擅长:python、mysql、java
<p>不幸的是,<code>DateFrame</code>ctor接受一个<code>dtype</code>描述符,但是您可以使用<code>read_csv</code>进行一些欺骗:</p>
<pre><code>In [143]:
import pandas as pd
import io
cols=["id", "name", "score", "height", "weight"]
df = pd.read_csv(io.StringIO(""), names=cols, dtype=dict(zip(cols,[int, str, int, float, float])), index_col=['id'])
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 0 entries
Data columns (total 4 columns):
name 0 non-null object
score 0 non-null int32
height 0 non-null float64
weight 0 non-null float64
dtypes: float64(2), int32(1), object(1)
memory usage: 0.0+ bytes
</code></pre>
<p>因此您可以看到数据类型是按需的,并且索引是按需设置的:</p>
<pre><code>In [145]:
df.index
Out[145]:
Int64Index([], dtype='int64', name='id')
</code></pre>