2017-04-26 88 views
1

對於我的數據集我想,以取代在數據幀的第一列的自動索引與數據框的所有列名設置新的索引標題行內。熊貓指數冠軍,列標題

原始數據集:

import numpy as np 
import pandas as pd 
df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6]]),columns=['a','b','c']) 
df 

    a b c 
0 1 2 3 
1 4 5 6 

將指數作爲第一列:

df.set_index('a',inplace=True) 
df 

    b c 
a 
1 2 3 
4 5 6 

不過,現在我被困在如何與列標題拿到指標標題在線。下面是輸出我要找:

a b c 
1 2 3 
4 5 6 

回答

2

使用這一招,但我認爲這不是最好的做法:

df.columns.name = df.index.name 
df.index.name = None 
print (df) 
a b c 
1 2 3 
4 5 6 
+0

這個作品,謝謝! – Ariel