<p>您可以首先从<code>tuples</code>创建新列,然后使用<code>aggfunc=sum</code>创建<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.melt.html" rel="nofollow">^{<cd2>}</a>,最后创建<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.pivot_table.html" rel="nofollow">^{<cd3>}</a>:</p>
<pre><code>df['a'],df['b'],df['c'],df['d'] = zip(*df.index)
print (df)
Pr(X,O) a b c d
(H, H, H, H) 0.042743 H H H H
(H, H, H, C) 0.003664 H H H C
(H, H, C, H) 0.073274 H H C H
(H, H, C, C) 0.021982 H H C C
(H, C, H, H) 0.005234 H C H H
(H, C, H, C) 0.000449 H C H C
(H, C, C, H) 0.031403 H C C H
(H, C, C, C) 0.009421 H C C C
(C, H, H, H) 0.113982 C H H H
(C, H, H, C) 0.009770 C H H C
(C, H, C, H) 0.195398 C H C H
(C, H, C, C) 0.058619 C H C C
(C, C, H, H) 0.048849 C C H H
(C, C, H, C) 0.004187 C C H C
(C, C, C, H) 0.293096 C C C H
(C, C, C, C) 0.087929 C C C C
print (pd.melt(df, id_vars=['Pr(X,O)'])
.pivot_table(index='value', columns='variable', values='Pr(X,O)', aggfunc=sum))
variable a b c d
value
C 0.81183 0.480568 0.771122 0.196021
H 0.18817 0.519432 0.228878 0.803979
</code></pre>
<p>或者如果需要列名作为<code>int</code>并删除索引和列名,请使用<a href="http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#changes-to-rename" rel="nofollow">^{<cd6>}</a>(在<code>pandas</code><code>0.18.0</code>中新增):</p>
<pre><code>print (pd.melt(df, id_vars=['Pr(X,O)'])
.pivot_table(index='value', columns='variable', values='Pr(X,O)', aggfunc=sum)
.rename_axis(None)
.rename_axis(None, axis=1))
0 1 2 3
C 0.81183 0.480568 0.771122 0.196021
H 0.18817 0.519432 0.228878 0.803979
</code></pre>