October 2019
Beginner to intermediate
674 pages
16h 43m
English
The swaplevel function enables levels within the multi-index to be swapped:
In [281]: swappedDF=sharesIndexDF[:7].swaplevel(0, 1, axis=0)
swappedDF Out[281]: Nasdaq S&P 500 Russell 2000
PriceType TradingDate
open 2014/02/21 4282.17 1841.07 1166.25
close 2014/02/21 4263.41 1836.25 1164.63
high 2014/02/21 4284.85 1846.13 1168.43
open 2014/02/24 4273.32 1836.78 1166.74
close 2014/02/24 4292.97 1847.61 1174.55
high 2014/02/24 4311.13 1858.71 1180.29
open 2014/02/25 4298.48 1847.66 1176.00
7 rows × 3 columns
The reorder_levels function is more general, allowing you to specify the order of the levels:
In [285]: reorderedDF=sharesIndexDF[:7].reorder_levels(['PriceType', 'TradingDate'],axis=0) reorderedDF Out[285]: ...
Read now
Unlock full access