import pandas as pd
# 构造数据
df = pd.DataFrame({'A': np.random.randint(3, size=(5, )), 'B': ['a', 'b', 'a', 'c', 'b'], 'C': np.random.randint(4, size=(5, ))})
# 透视表
df1 = df.pivot(index='A', columns='B', values='C')
# 重置索引
df1.reset_index(inplace=True)
df1.columns.name=None
1
2
3
4
5
6
7
8
9
10
11
12
df
df1
---------------------