答案 0 :(得分:2)
假设前六列是多索引,则不需要转置,而是取消堆栈:
df1 = df.unstack().fillna(0).astype(int)
# Count
#Parliamentarian: Vote 1 2 3
#Parliament Session Sitting Vote Party
#42 1 164 255 BQ 7 0 0
# C 0 81 0
# Lib 162 0 1
# NDP 0 34 1
# 256 BQ 10 0 0
# C 81 0 0
# Lib 0 164 1
# NDP 34 0 1
如果您愿意,可以添加第四列:
df1['Count',4] = 0