<p>您可以使用<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.pivot_table.html" rel="nofollow">^{<cd1>}</a>:</p>
<pre><code>df = df.pivot_table(index=['id','identifier'],
columns='name',
values='base_stat',
aggfunc='first')
print (df)
name Attack Defense HP Special Attack Special Defense Speed
id identifier
7 Squirtle 48 65 44 50 64 43
</code></pre>
<p>如果所有<code>DataFrames</code>都在列表<code>dfs</code>中,请将<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.concat.html" rel="nofollow">^{<cd4>}</a>与<code>list comprehension</code>一起使用:</p>
<pre><code>dfs = [df1, df2]
df = pd.concat([df.pivot_table(index=['id','identifier'],
columns='name',
values='base_stat',
aggfunc='first') for df in dfs])
print (df)
name Attack Defense HP Special Attack Special Defense Speed
id identifier
7 Squirtle 48 65 44 50 64 43
4 Charmander 52 43 39 60 50 65
</code></pre>
<p>上次将<code>reset_index</code>与<a href="http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#changes-to-rename" rel="nofollow">^{<cd7>}</a>一起使用(在<code>pandas</code><code>0.18.0</code>中新增),如果使用<code>pandas bellow 0.18.0</code>省略<code>rename_axis</code>并使用<code>df.columns.name = None</code>:</p>
<pre><code>df = pd.concat([df.pivot_table(index=['id','identifier'],
columns='name',
values='base_stat',
aggfunc='first') for df in dfs])
.reset_index()
.rename_axis(None, axis=1)
print (df)
id identifier Attack Defense HP Special Attack Special Defense Speed
0 7 Squirtle 48 65 44 50 64 43
1 4 Charmander 52 43 39 60 50 65
</code></pre>