2017-05-07 87 views
0

如何更改數據框的結構?我需要每一行的系列數據。我試圖拆散,但失敗了。如何更改數據框的結構?

實例數據框:

df: 

     c1 c2  c3 

    0  a  b  c 
    1  d  e  f 

輸出系列:

S1 
0 a 
1 b 
2 c 

S2 
0 d 
1 e 
2 f 

回答

0

可以移調數據幀,並且,如果你這麼喜歡,重命名的指數,在範圍:

dfT = df.T 
dfT.index = pd.RangeIndex(dfT.shape[0]) # Rename the indicies 
dfT.columns=['S{}'.format(i+1) for i in range(dfT.shape[1])] # Rename the columns 
dfT.unstack() 
+0

'pd.RangeIndex(dfT.shape [0])'更靈活。 – DyZ

+0

的確,我會更新我的答案。 – JohanL

+0

而'dfT.columns = ['S {}'。格式(i + 1)爲我在範圍內(dfT.shape [1])]''照顧列。 – DyZ