答案 0 :(得分:6)
设置index.names
(复数,因为MultiIndex
)或使用rename_axis
:
df.index.names = ['Ticker','date']
#if want extract second name
df.index.names = ['Ticker',df.index.names[1]]
或者:
df = df.rename_axis(['Ticker','date'])
#if want extract second name
df = df.rename_axis(['Ticker',df.index.names[1]])
<强>示例强>:
mux = pd.MultiIndex.from_product([['NAVER'], ['2018-11-28','2018-12-01','2018-12-02']],
names=[None, 'date'])
df = pd.DataFrame({'open':[1,2,3]},
index=mux)
print(df)
open
date
NAVER 2018-11-28 1
2018-12-01 2
2018-12-02 3
df = df.rename_axis(['Ticker','date'])
print (df)
open
Ticker date
NAVER 2018-11-28 1
2018-12-01 2
2018-12-02 3