Table of contents
Introduction
Series are 1d anddata frames are 2d
The reason behind this if we want to fetch a value in series we will have to give only one value
Whereas in a series we will have to give 2 values --- row and column
We can create a series where the index is two values
But the problem is we can only give one value as the index
To solve this we use multindex
Creating a multindex
The
MultiIndex
object is the hierarchical analogue of the standardIndex
object which typically stores the axis labels in pandas objects.You can think of
MultiIndex
as an array of tuples where each tuple is unique. AMultiIndex
can be created from a list of arrays (usingMultiIndex.from_arrays()
), an array of tuples (usingMultiIndex.from_tuples()
), a crossed set of iterables (usingMultiIndex.from_product()
), or aDataFrame
(usingMultiIndex.from_frame()
).The
Index
constructor will attempt to return aMultiIndex
when it is passed a list of tuples. The following examples demonstrate different ways to initialize MultiIndexes.
Unstacking
It will convert a multi-index series to a dataframe
Always the inner index will be spread as columns
Stacking
- Exact Opposite of unstacking is called as stacking