2017-08-14 115 views
2

有一個DF爲:修改矩陣DF格式

14  15   16 
14 10.1166 18.2331 65.0185 
15 18.2331 6.664  57.5195 
16 65.3499 57.851  20.9907 

什麼是不同的更有效的方式來修改DF看作

a b c 
0 14 14 10.1166 
1 14 15 18.2331 
2 14 16 65.0185 
3 15 14 18.2331 
4 15 15 6.664 
etc. 

我寫了這個代碼,但我不喜歡的事實上,我需要使用它的循環。

for row in tt.index: 
    row_vals = tt[tt.index==row] 
    col_vals = row_vals.T 
    col_vals['from_zone'] = row 
    col_vals['to_zone'] = tt.index 
    col_vals['travel_time'] = col_vals[row].astype('int') 
    col_vals = col_vals.drop(row, axis=1) 
    travel_data = pd.concat([travel_data,col_vals]) 

回答

3
In [58]: df.stack().reset_index().rename(columns={'level_0':'a','level_1':'b',0:'c'}) 
Out[58]: 
    a b  c 
0 14 14 10.1166 
1 14 15 18.2331 
2 14 16 65.0185 
3 15 14 18.2331 
4 15 15 6.6640 
5 15 16 57.5195 
6 16 14 65.3499 
7 16 15 57.8510 
8 16 16 20.9907 

循序漸進:

In [59]: df.stack() 
Out[59]: 
14 14 10.1166 
    15 18.2331 
    16 65.0185 
15 14 18.2331 
    15  6.6640 
    16 57.5195 
16 14 65.3499 
    15 57.8510 
    16 20.9907 
dtype: float64 

In [60]: df.stack().reset_index() 
Out[60]: 
    level_0 level_1  0 
0  14  14 10.1166 
1  14  15 18.2331 
2  14  16 65.0185 
3  15  14 18.2331 
4  15  15 6.6640 
5  15  16 57.5195 
6  16  14 65.3499 
7  16  15 57.8510 
8  16  16 20.9907 
1

使用stackrename列名

In [620]: df.stack().reset_index(name='c').rename(columns={'level_0': 'a', 'level_1': 'b'}) 
Out[620]: 
    a b  c 
0 14 14 10.1166 
1 14 15 18.2331 
2 14 16 65.0185 
3 15 14 18.2331 
4 15 15 6.6640 
5 15 16 57.5195 
6 16 14 65.3499 
7 16 15 57.8510 
8 16 16 20.9907 
1

讓我們用rename_axisreset_indexmelt

df.rename_axis('a').reset_index().melt(id_vars='a',var_name='b',value_name='c') 

輸出:

a b  c 
0 14 14 10.1166 
1 15 14 18.2331 
2 16 14 65.3499 
3 14 15 18.2331 
4 15 15 6.6640 
5 16 15 57.8510 
6 14 16 65.0185 
7 15 16 57.5195 
8 16 16 20.9907