2017-07-28 150 views
2

在我的熊貓數據幀我已經從一個CSV裝在下表中,稱之爲table.csv編輯熊貓透視表

x   | w   | y | z 
indexval1 | stringval1 | b1 | c1 
indexval2 | stringval2 | b2 | c2 
indexval3 | stringval3 | b3 | c3 
indexval4 | stringval4 | b4 | c4 

這是我跑轉動代碼...

df = pandas.read_csv("table.csv") 
df.pivot_table(values = ["y", "z"] index = "x" columns = "w") 
df.fillna(value = 00) 

,看起來像這樣:

----- | y   | z   | y   | z 
w  |stringval1 | stringval1 | stringval2 | stringval2 
x  |   |   |   | 
indexval1| b1  | c1   | 00   | 00 
indexval2| 00  | 00   | b2   | c2 
indexval3| 00  | 00   | 00   | 0 
indexval4| 00  | 00   | 00   | 00 

如何修改它看起來像THI S:

w  |stringval1 | stringval1 | stringval2 | stringval2 
    ----- | y   | z   | y   | z 
    x  |   |   |   | 
    indexval1| b1  | c1   | 00   | 00 
    indexval2| 00  | 00   | b2   | c2 
    indexval3| 00  | 00   | 00   | 0 
    indexval4| 00  | 00   | 00   | 00 

回答

3

我想你需要swaplevel,因爲列是Multiindex

df = df.swaplevel(0,1, axis=1) 
+1

甜的感謝!將標記爲正確的時間 – Travis