python - How do I change order/grouping/level of pandas MultiIndex columns? -


i'm trying reorder/swaplevel/pivot/something columns in pandas dataframe. columns multiindex, can't find sauce want.

the fastest varying column in multiindex month, slowest varying column.

i've got nbviewer notebook if try out yourself: http://nbviewer.ipython.org/gist/flamingbear/4cfac24c80fe34a67474

what have:

+-------------------------------------------------------------------+ |+-----+------+------+-----+------+-----+-----+------+-----+-----+  | ||     |weight             |extent            |rank                || |+-----+------+------+-----+------+-----+-----+------+-----+-----+  | ||month|'1jan'|'feb' |'mar'|'1jan'|'feb'|'mar'|'1jan'|'feb'|'mar'|  | |+-----+------+------+-----+------+-----+-----+------+-----+-----+  | ||year |      |      |     |      |     |     |      |     |     |  | |+-----+------+------+-----+------+-----+-----+------+-----+-----+  | ||2000 |45.1  |46.1  |25.1 |13.442|14.94|15.02|13    |17   |14   |  | |+-----+------+------+-----+------+-----+-----+------+-----+-----+  | ||2001 |85.0  |16.0  |49.0 |13.380|14.81|15.14|12    |15   |17   |  | |+-----+------+------+-----+------+-----+-----+------+-----+-----+  | ||2002 |90.0  |33.0  |82.0 |13.590|15.13|14.88|15    |22   |10   |  | |+-----+------+------+-----+------+-----+-----+------+-----+-----+  | ||2003 |47.0  |34.0  |78.0 |13.640|14.83|15.27|17    |16   |22   |  | |+-----+------+------+-----+------+-----+-----+------+-----+-----+  | +-------------------------------------------------------------------+ 

what want

+------------------------------------------------------------------+ |+-----+------+------+----+------+------+-----+------+------+----+ | ||month|1jan              |feb                |mar                || |+-----+------+------+----+------+------+-----+------+------+----+ | ||     |weight|extent|rank|weight|extent|rank |weight|extent|rank| | |+-----+------+------+----+------+------+-----+------+------+----+ | ||year |      |      |    |      |      |     |      |      |    | | |+-----+------+------+----+------+------+-----+------+------+----+ | ||2000 |45.1  |13.442|13  |46.1  |14.94 |17   | 25.1 |15.02 |14  | | |+-----+------+------+----+------+------+-----+------+------+----+ | ||2001 |85.0  |13.380|12  |16.0  |14.81 |15   | 49.0 |15.14 |17  | | |+-----+------+------+----+------+------+-----+------+------+----+ | ||2002 |90.0  |13.590|15  |33.0  |15.13 |22   | 82.0 |14.88 |10  | | |+-----+------+------+----+------+------+-----+------+------+----+ | ||2003 |47.0  |13.640|17  |34.0  |14.83 |16   | 78.0 |15.27 |22  | | |+-----+------+------+-----------+------+-----+------+------+----+ | +------------------------------------------------------------------+ 

any appreciated. can work original dataframe, writing csv desired ordering fantastic.

thanks in advance, matt

your columns multiindex. need reassign dataframe's columns new multiindex created swapping levels of existing one:

df.columns = df.columns.swaplevel(0, 1) df.sortlevel(0, axis=1, inplace=true) >>> df  month   '1jan'                 'feb'                 'mar'                       weight  extent  rank  weight  extent  rank  weight  extent  rank year                                                                     2000      45.1  13.442    13    46.1   14.94    17    25.1   15.02    14 2001      85.0  13.380    12    16.0   14.81    15    49.0   15.14    17 2002      90.0  13.590    15    33.0   15.13    22    82.0   14.88    10 2003      47.0  13.640    17    34.0   14.83    16    78.0   15.27    22 

you can export csv:

df.to_csv(filename) 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -