hierarchical indexing provides consistent way to rearrange data in dataframe there are two primary actionsstackthis "rotatesor pivots from the columns in the data to the rows unstackthis pivots from the rows into the columns 'll illustrate these operations through series of examples consider small dataframe with string arrays as row and column indexesin [ ]data dataframe(np arange( reshape(( ))index=pd index(['ohio''colorado']name='state')columns=pd index(['one''two''three']name='number')in [ ]data out[ ]number one state ohio colorado two three using the stack method on this data pivots the columns into the rowsproducing seriesin [ ]result data stack(in [ ]result out[ ]state number ohio one two three colorado one two three from hierarchically-indexed seriesyou can rearrange the data back into dataframe with unstackin [ ]result unstack(out[ ]number one two three state ohio colorado by default the innermost level is unstacked (same with stackyou can unstack different level by passing level number or namein [ ]result unstack( out[ ]state ohio colorado number one in [ ]result unstack('state'out[ ]state ohio colorado number one data wranglingcleantransformmergereshape www it-ebooks info