<p>只是为了进一步解释尼廷的回答<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.set_index.html#pandas.DataFrame.set_index" rel="noreferrer">^{<cd1>}</a>:</p>
<pre><code>In [100]:
df.set_index('head0')
Out[100]:
head1 head2 head3
head0
bar 32 3 100
bix 22 NaN NaN
foo 11 1 NaN
qux NaN 10 NaN
xoo NaN 2 20
</code></pre>
<p>请注意,这将返回df,因此您要么像这样分配给df:<code>df = df.set_index('head0')</code>,要么设置参数<code>inplace=True</code>:<code>df.set_index('head0', inplace=True)</code></p>
<p>也可以直接分配给索引:</p>
<pre><code>In [99]:
df.index = df['head0']
df
Out[99]:
head0 head1 head2 head3
head0
bar bar 32 3 100
bix bix 22 NaN NaN
foo foo 11 1 NaN
qux qux NaN 10 NaN
xoo xoo NaN 2 20
</code></pre>
<p>请注意,执行上述操作将要求您删除无关的“head0”列,可以通过调用<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.drop.html#pandas.DataFrame.drop" rel="noreferrer">^{<cd5>}</a>来完成,例如:<code>df.drop('head0', axis=1)</code></p>