Edit Pandas Pivot Table -


in pandas dataframe have following table loaded csv, call table.csv

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

here code run pivot...

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

which looks this:

-----    | 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   

how can edit this:

    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   

i think need swaplevel, because columns multiindex:

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

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -