# Pandas — Hierarchical Index & Cross-section

## Open your Colab notebook and here are the follow-up exercises! — #PySeries#Episode 10

`print(‘Hello, Advanced Pandas: Hierarchical Index & Cross-section!’)`

Initializing a multi-level DataFrame:

`import numpy as np`

import pandas as pd

from numpy.random import randn

np.random.seed(101)

## Pandas — MultiIndex & Advanced Index

As a convenience, we can pass a list of arrays directly into a special method below to construct a MultiIndex automatically:

outside=[‘G1’,’G1',’G1',’G2',’G2',’G2']

inside=[1,2,3,1,2,3]

hier_index=list(zip(outside, inside))

hier_index=pd.MultiIndex.from_tuples(hier_index)df=pd.DataFrame(randn(6,2), hier_index, [‘A’, ‘B’])df

The reason that the MultiIndex matters is that it can allow you to do grouping, selection, and reshaping operations such as:

## Calling Data:

# If we want everything that is under ‘G1’, type df.loc():

# We will get a sub-set ‘G1’ of the DataFrame; check it out:df.loc[‘G1’]

We can continue to indexing off this, going deeper…

`df.loc[‘G1’].loc[1]`

And deeper, and deeper…

The basic idea we can from the outside index continue calling inside deeper!

df.loc['G1'].loc[1][0]2.706849839399938

## Naming Columns:

# Indexes do not have any namesdf.index.namesFrozenList([None, None])

But you can pass a list of names, for instance:

df.index.names = [‘Groups’, ‘Nums’]

# Now when we call it we have the outside label

# as ‘Groups’ and inside ‘Nums’df

## Location and grabbing — Cross-Section:

Location and grab the group ‘G2’, nuns 2, ‘B’ column like this:

df.loc[‘G2’].loc[2][‘B’]

##Cross Section — Multi Level Index

# Let’s say we want to grab everything under ‘Nums’ = 1

# with both groups;

# What differentiates it from the loc method is that

# we can skip or go inside the multi-level index

# This is to say: grab a cross-section where the

# level is equal 1 and level is equal ‘Nums’df.xs(1, level=’Nums’)

`print(“Thank you for Reading this Post! See you in the next `**PySeries **Episode o/”)

Colab File link:)

# Posts Related:

00Episode#**PySeries **— Python — Jupiter Notebook Quick Start with VSCode — How to Set your Win10 Environment to use Jupiter Notebook

01Episode#**PySeries **— Python — Python 4 Engineers — Exercises! An overview of the Opportunities Offered by Python in Engineering!

02Episode#**PySeries **— Python — Geogebra Plus Linear Programming- We’ll Create a Geogebra program to help us with our linear programming

03Episode#**PySeries** — Python — Python 4 Engineers — More Exercises! — Another Round to Make Sure that Python is Really Amazing!

04Episode#**PySeries** — Python — Linear Regressions — The Basics — How to Understand Linear Regression Once and For All!

05Episode#**PySeries** — Python — NumPy Init & Python Review — A Crash Python Review & Initialization at NumPy lib.

06Episode#**PySeries** — Python — NumPy Arrays & Jupyter Notebook — Arithmetic Operations, Indexing & Slicing, and Conditional Selection w/ np arrays.

07Episode#**PySeries** — Python — Pandas — Intro & Series — What it is? How to use it?

08Episode#**PySeries** — Python — Pandas DataFrames — The primary Pandas data structure! It is a dict-like container for Series objects

09Episode#**PySeries** — Python — Python 4 Engineers — Even More Exercises! — More Practicing Coding Questions in Python!

10Episode#**PySeries** — Python — Pandas — Hierarchical Index & Cross-section — Open your Colab notebook and here are the follow-up exercises!

11Episode#**PySeries** — Python — Pandas — Missing Data — Let’s Continue the Python Exercises — Filling & Dropping Missing Data

12Episode#**PySeries** — Python — Pandas — Group By — Grouping large amounts of data and compute operations on these groups

13Episode#**PySeries** — Python — Pandas — Merging, Joining & Concatenations — Facilities For Easily Combining Together Series or DataFrame

14Episode#**PySeries** — Python — Pandas — Pandas Dataframe Examples: Column Operations

15Episode#**PySeries** — Python — **Python 4 Engineers **— Keeping It In The Short-Term Memory — **Test Yourself!** Coding in Python, Again!

16Episode#**PySeries** — NumPy — NumPy Review, Again;)** **— Python Review Free Exercises

17Episode#**PySeries** — Generators in Python** **— Python Review Free Hints

18Episode#**PySeries** — Pandas Review…Again;) — Python Review Free Exercise

19Episode#**PySeries** — MatlibPlot & Seaborn Python Libs — Reviewing theses Plotting & Statistics Packs

20Episode#**PySeries** — Seaborn Python Review — Reviewing theses Plotting & Statistics Packs