Locked lesson.
About this lesson
Let's discuss multi-indexes: what they are, how they work, and how to select data from them.
Exercise files
Download this lesson’s related exercise files.
Selecting Specific Cells From Multi-Index.docx56.8 KB Selecting Specific Cells From Multi-Index - Solution.docx
58.8 KB
Quick reference
Selecting Specific Cells From Multi-Index
Multi-Index DataFrames are DataFrames with multiple indexes (row headers).
When to use
Use them to further categorize your row headers into different categories.
Instructions
To create a multi-index:
outer = ['A1', 'A1', 'A1', 'A2', 'A2', 'A2'] # Create Outer Index
inner = [1,2,3,1,2,3] # Create Inner Index
multi = list(zip(outer,inner))
multi = pd.MultiIndex.from_tuples(multi)
new_df = pd.DataFrame(randn(6,2), multi, ['Mon', 'Fri'])
To pull data from a multi-index dataframe:
new_df.loc['A1'] #pull data from one outer index
new_df.loc['A1'].loc[2] # pull data from a specific inner row of an outer index
new_df.loc['A1'].loc[2].loc['Tues'] # pull one specific point of data
Hints & tips
- Multi-Index Data Frames let us further categories our rows
- Don't get hung up on them :-)
Lesson notes are only available for subscribers.