Get startedGet started for free

Working with multiple levels

1. Working with multiple levels

In previous videos, we have learned

2. Review

to stack and unstack the innermost row or column level, to choose which level to move using its number or name, and to rearrange a level by chaining the stacking and unstacking processes. But, rearranging one level at a time has limitations.

3. Rearranging multiple levels

So, we'll learn now how to swap levels, and how to stack and unstack multiple levels at the same time.

4. Swap levels

The swaplevel() method can switch the order of two levels within the same axis. This means that we can swap the order of two row levels or two column levels.

5. Swap levels

Let's work with the following stacked DataFrame containing data about cars.

6. Swap levels

We apply the swaplevel() method passing the index zero and two. In the output, we can see how the first and third row levels are now interchanged.

7. Swap levels and unstack

We can now chain it with the unstacking process. We can see that the row level containing the price and sold features was moved to the column index. If we haven't changed the order of the levels, the unstacked level would have been the brand level.

8. Swap levels and unstack

In the following example, we first unstack the last row index level,

9. Swap levels and unstack

then swap the first and second column levels. We do this by setting the axis parameter to 1. We can see how the year appears on top of the brand level.

10. Swap levels and stack

Finally, we can also stack the column index of cars,

11. Swap levels and stack

then call the swaplevel() method, passing zero and two as arguments. In the output, we can see how the recently stacked level and the original first level are switched.

12. Multiple levels

The DataFrame here has multi-level indexes on the rows and on the columns. So, how do we reshape any of these multiple levels at the same time?

13. Unstacking multiple levels

Let's work again with the cars DataFrame. It has a multi-index on the rows. In particular, it has three levels.

14. Unstacking levels by number

Unstacking several levels at the same time is easy. We just have to pass a list of the index numbers to the level parameter. In the output, we see that the first and second row levels are now on the column index. The resulting DataFrame has three levels on the row indices.

15. Unstacking levels by name

We could also use the level names by passing a list of brand and model levels to unstack(). As a result, we get the same DataFrame as before.

16. Stacking multiple levels

Let's take this last DataFrame.

17. Stacking by name or number

We use the same syntax to stack several levels. We could pass a list of index numbers or their respective names. In both cases, we get a resulting DataFrame where the year and brand levels are now in the row indices. It's important to notice that the order in which you pass the names matters. In the example, we pass year and then brand, so the innermost level will be the brand level.

18. Let's practice!

Now, you can start rearranging multiple levels at the same time!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.