Book Image

Learning Pandas

By : Michael Heydt
Book Image

Learning Pandas

By: Michael Heydt

Overview of this book

Table of Contents (19 chapters)
Learning pandas
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Hierarchical indexing


Hierarchical indexing is a feature of pandas that allows specifying two or more index levels on an axis. The specification of multiple levels in an index allows for efficient selection of subsets of data. A pandas index that has multiple levels of hierarchy is referred to as a MultiIndex.

We can demonstrate creating a MultiIndex using the sp500 data. Suppose we want to organize this data by both the Sector and Symbol. We can accomplish this with the following code:

In [107]:
   # first, push symbol into a column
   reindexed = sp500.reset_index()
   # and now index sp500 by sector and symbol
   multi_fi = reindexed.set_index(['Sector', 'Symbol'])
   multi_fi

Out[107]:
                                   Price  BookValue
   Sector                 Symbol                   
   Industrials            MMM     141.14     26.668
   Health Care            ABT      39.60     15.573
                          ABBV     53.95      2.954
   Information Technology ACN      79.79  ...