There's more...

To help further understand stack/unstack, let's use them to transpose the college DataFrame.

In this context, we are using the precise mathematical definition of the transposing of a matrix, where the new rows are the old columns of the original data matrix.

If you take a look at the output from step 2, you'll notice that there are two index levels. By default, the unstack method uses the innermost index level as the new column values. Index levels are numbered beginning from zero from the outside. Pandas defaults the level parameter of the unstack method to -1, which refers to the innermost index. We can instead unstack the outermost column using level=0:

>>> college.stack().unstack(0)

There is actually a very simple way to transpose a DataFrame that don't require stack or unstack by using the transpose method or the T attribute like this:

>>> college.T
>>> college.transpose()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset