MultiIndex Series & DataFrame

MultiIndex Series & DataFrame

Introduction

  1. Series are 1d anddata frames are 2d

  2. The reason behind this if we want to fetch a value in series we will have to give only one value

  3. Whereas in a series we will have to give 2 values --- row and column

  4. We can create a series where the index is two values

  5. But the problem is we can only give one value as the index

  6. To solve this we use multindex

Creating a multindex

  1. The MultiIndex object is the hierarchical analogue of the standard Index object which typically stores the axis labels in pandas objects.

  2. You can think of MultiIndex as an array of tuples where each tuple is unique. A MultiIndex can be created from a list of arrays (using MultiIndex.from_arrays()), an array of tuples (using MultiIndex.from_tuples()), a crossed set of iterables (using MultiIndex.from_product()), or a DataFrame (using MultiIndex.from_frame()).

  3. The Index constructor will attempt to return a MultiIndex

  4. when it is passed a list of tuples. The following examples demonstrate different ways to initialize MultiIndexes.

Unstacking

  1. It will convert a multi-index series to a dataframe

  2. Always the inner index will be spread as columns

Stacking

  1. Exact Opposite of unstacking is called as stacking