简短回答:是的。
请参考https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html
s = pd.Series([1, 2, 3, 4])
df.set_index([s, s**2])
month year sale
1 1 1 2012 55
2 4 4 2014 40
3 9 7 2013 84
4 16 10 2014 31
多索引的另一个例子:
dct = {'id' : (0,0,1,1,2,2,3,3,4,4,5,5),
'Store': ('A','A','A','A','A','A','B','B','B','C','C','C'),
'code_num':('INC101','INC102','INC103','INC104','INC105','INC106','INC201','INC202','INC203','INC301','INC302','INC303'),
'days':('4','18','9','15','3','6','10','5','3','1','8','5'),
'products': ('remote','antenna','remote,antenna','TV','display','TV','display,touchpad','speaker','Cell','display','speaker','antenna')
}
df = pd.DataFrame(dct)
df.set_index(['id','Store','code_num'])
df :