1. What’s MultiIndex?

We have mentioned that single level index uses a series of labels to uniquely identify each row or column. Unlike the single level index, the multi-index uses a series of tuples with each uniquely identifying a row or column. For the simplicity of the terminology, we’ll just focus on the rows’ index, but the same rules can apply to columns too.

Multi-Level Indexing

As shown above, we can access the index property of a DataFrame object. You may notice that we get the index as a MultiIndex object, which is a multi-level or hierarchical index object for pandas DataFrame or Series. This object has three key attributes: names, levels, and codes. Let’s review them.

>>> df_mean.index.names
FrozenList(['embark_town', 'class', 'sex'])
>>> df_mean.index.levels
FrozenList([['Cherbourg', 'Queenstown', 'Southampton'], ['First', 'Second', 'Third'], ['female', 'male']])
>>> df_mean.index.codes
FrozenList([[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2], [0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]])
  • names: our index has three names for each of the index levels: embark_town, class, and sex.
  • levels: the labels for each level. embark_town has three labels, class has three labels, and sex has two labels.
  • codes: the integers to indicate the index for each level. For instance, the first list in the FrozenList indicates all the rows’ embark_town level (0 -> ‘Cherbourg’, 1 -> ‘Queenstown’, and 2 -> ‘Southampton’).

Continue reading: https://towardsdatascience.com/confused-by-multi-index-in-pandas-9-essential-operations-to-know-e6aec29ee6d8?source=rss—-7f60cf5620c9—4

Source: towardsdatascience.com