From the course: pandas Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Using unstack()

Using unstack() - pandas Tutorial

From the course: pandas Essential Training

Start my 1-month free trial

Using unstack()

- [Instructor] With unstack, we return a series or a DataFrame. Because the NAN values, or the missing data values are displayed, you have the option to fill them with another value. So, in the example of the Olympic data set that we are working on, it would make sense to have every NAN value to be zero, because that means that there were no medals won for that event. With unstacking, you can pivot a level of the index labels, and this returns a DataFrame having a new level of column labels. If the index isn't a MultiIndex, the output will be a series, and the level involved will automatically get sorted. Let's head over to our Jupyter Notebook to look at unstacking. So, remember that our original DataFrame is df, and, when we do an unstack, what we're looking at is to try and get a wider DataFrame. So, if i do a df.unstack, I expect this original DataFrame, df, to widen out. And, as with stacking, I prefer to explicitly state which column I'm doing the unstacking on. In this case…

Contents