three two three unstacking might introduce missing data if all of the values in the level aren' found in each of the subgroupsin [ ] series([ ]index=[' '' '' '' ']in [ ] series([ ]index=[' '' '' ']in [ ]data pd concat([ ]keys=['one''two']in [ ]data unstack(out[ ] one nan two nan nan stacking filters out missing data by defaultso the operation is easily invertiblein [ ]data unstack(stack(out[ ]one two in [ ]data unstack(stack(dropna=falseout[ ]one nan two nan nan when unstacking in dataframethe level unstacked becomes the lowest level in the resultin [ ]df dataframe({'left'result'right'result }columns=pd index(['left''right']name='side')in [ ]df out[ ]side state number ohio one two three colorado one two three left right in [ ]df unstack('state'out[ ]side left right state ohio colorado ohio number one two colorado in [ ]df unstack('state'stack('side'out[ ]state ohio colorado number side one left right two left reshaping and pivoting www it-ebooks info