用pivot_table函数。
import pandas as pd
df = pd.DataFrame({'letter': ['A']*3+['B']*4+['C']*3, 'class': [1]*6+[2]+[1]*2+[3], 'stem': ['甲', '乙', '丙', '甲', '乙', '丙','丁','乙','丙','戊'], 'number': [12, 432, 32,23, 12, 45, 67, 34, 1245, 456]})
d=pd.pivot_table(df,values=['number'],index=['letter'],columns=['class','stem'],dropna=True)
print(d)
补充:dropna=True是默认参数,添加fill_value='',没有数据的项就会空着。