<p>多亏了@Terry的解决方案。我只添加了<code>fillna</code>方法</p>
<pre><code>from pandas import DataFrame
if __name__ == '__main__':
data = {'Index': [3, 4,
5, 6,
7, 3,
4, 5,
6, 7,
3, 4,
5, 6],
'Name': ['A', 'B',
'C', 'D',
'E', 'A',
'B', 'C',
'D', 'E',
'A', 'B',
'C', 'D'],
'Value': ['xyz', 'asd',
'cvz', 'red',
'fsd', 'gfd',
'gsa', 'hgd',
'ewr', 'dgs',
'dfd', 'dsa',
'dgd', 'dwr']}
df = DataFrame(data, columns=['Index',
'Name',
'Value'])
df['Index'] = df.groupby(['Index', 'Name'])['Index'].cumcount()
print(df.pivot(index='Index',
columns='Name',
values='Value').reset_index(drop=True).fillna(''))
</code></pre>
<p>输出:</p>
<pre><code>Name A B C D E
0 xyz asd cvz red fsd
1 gfd gsa hgd ewr dgs
2 dfd dsa dgd dwr
</code></pre>