Session Ready
Exercise

Stacking & unstacking I

You are now going to practice stacking and unstacking DataFrames. The users DataFrame you have been working with in this chapter has been pre-loaded for you, this time with a MultiIndex. Explore it in the IPython Shell to see the data layout. Pay attention to the index, and notice that the index levels are ['city', 'weekday']. So 'weekday' - the second entry - has position 1. This position is what corresponds to the level parameter in .stack() and .unstack() calls. Alternatively, you can specify 'weekday' as the level instead of its position.

Your job in this exercise is to unstack users by 'weekday'. You will then use .stack() on the unstacked DataFrame to see if you get back the original layout of users.

Instructions
100 XP
  • Define a DataFrame byweekday with the 'weekday' level of users unstacked.
  • Print the byweekday DataFrame to see the new data layout. This has been done for you.
  • Stack byweekday by 'weekday' and print it to check if you get the same layout as the original users DataFrame.