From the course: pandas Essential Training

Using stack() - pandas Tutorial

From the course: pandas Essential Training

Start my 1-month free trial

Using stack()

- [Teacher] When using the stack function, the stack function returns a data frame or a series. And notice that you will never have any any values or missing data because by default the drop any flag is equal to true. The inner levels of a stack function are sorted. So when we do a stack we are returning a data frame or series with a new innermost level of rules. So the hundred meters and the two hundred meters which are the inner most columns are dragged down and if there's 100 or 200 meter that corresponds to each male or female athlete then this is listed. To help you remember the stack function, I try and visualize whether I want to make this data frame taller or wider. If I want to make the data frames taller, then I need to use the stack function because stacking gives you a taller data frame. Let's head over to the Jupiter notebook to look at stacking. Now remember that our original data frame is called DF and if we then us the stack function we are going to end up with the taller or longer data frame. So df.stack and we can see that the corresponding data frame that's been generated is taller. Now in the real world, I prefer to actually specify which column I'm going to be using in the stacking function. So I would normally say df.stack and specify the event and I'll get exactly the same data frame. In the next video, we'll cover unstack.

Contents