avatarJ3

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3903

Abstract

ber">706849839399938</span></pre></div><h2 id="6e42">Naming Columns:</h2><div id="9a87"><pre><span class="hljs-comment"># Indexes do not have any names</span></pre></div><div id="84a9"><pre>df.<span class="hljs-keyword">index</span>.names</pre></div><div id="b71e"><pre><span class="hljs-function"><span class="hljs-title">FrozenList</span><span class="hljs-params">([None, None])</span></span></pre></div><p id="ceed">But you can pass a list of names, for instance:</p><div id="699c"><pre>df.<span class="hljs-keyword">index</span>.names = [‘<span class="hljs-keyword">Groups</span>’, ‘Nums’]

Now <span class="hljs-keyword">when</span> we <span class="hljs-keyword">call</span> it we have the outside label

<span class="hljs-keyword">as</span><span class="hljs-keyword">Groups</span><span class="hljs-keyword">and</span> inside ‘Nums’</pre></div><div id="c688"><pre><span class="hljs-built_in">df</span></pre></div><figure id="4919"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*lKn9xABE3pIxvaqzOcrZ-g.png"><figcaption></figcaption></figure><h2 id="d831">Location and grabbing — Cross-Section:</h2><p id="7a6c">Location and grab the group ‘G2’, nuns 2, ‘B’ column like this:</p><div id="adc8"><pre>df.loc[‘G2’].loc[<span class="hljs-number">2</span>][‘B’]

##<span class="hljs-keyword">Cross</span> Section — Multi <span class="hljs-keyword">Level</span> <span class="hljs-keyword">Index</span>

Let’s say we want <span class="hljs-keyword">to</span> grab everything under ‘Nums’ = <span class="hljs-number">1</span>

<span class="hljs-keyword">with</span> <span class="hljs-keyword">both</span> <span class="hljs-keyword">groups</span>;

What differentiates it <span class="hljs-keyword">from</span> the loc <span class="hljs-keyword">method</span> <span class="hljs-keyword">is</span> that

we can skip <span class="hljs-keyword">or</span> go inside the multi-<span class="hljs-keyword">level</span> <span class="hljs-keyword">index</span>

This <span class="hljs-keyword">is</span> <span class="hljs-keyword">to</span> say: grab a <span class="hljs-keyword">cross</span>-section <span class="hljs-keyword">where</span> the

<span class="hljs-keyword">level</span> <span class="hljs-keyword">is</span> equal <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">level</span> <span class="hljs-keyword">is</span> equal ‘Nums’</pre></div><div id="986e"><pre>df.xs(1, <span class="hljs-attribute">level</span>=’Nums’)</pre></div><figure id="491c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*y2VDm0BEBzbzkshOOjYb5g.png"><figcaption></figcaption></figure><div id="8dc1"><pre><span class="hljs-keyword">print</span>(“Thank you <span class="hljs-keyword">for</span> Reading <span class="hljs-keyword">this</span> Post! See you <span class="hljs-keyword">in</span> the <span class="hljs-keyword">next</span> PySeries Episode o/”)</pre></div><p id="1ab2">Colab File <a href="https://colab.research.google.com/drive/1I1Mixbf_x838j7aMOO7lO3APl3oLk0zd?usp=sharing">link</a>:)</p><h1 id="c92c">Posts Related:</h1><p id="e2d7">00Episode#<b>PySeries </b>— Python — <a href="https://medium.com/@J.3/python-jupiter-notebook-quick-start-with-vscode-916c43c10d9a">Jupiter Notebook Quick Start with VSCode — How to Set your Win10 Environment to use Jupiter Notebook</a></p><p id="6c67">01Episode#<b>PySeries </b>— Python — <a href="https://readmedium.com/python-for-engenniging-exercises-977fbe4d6d02">Python 4 Engineers — Exercises! An overview of the Opportunities Offered by Python in Engineering!</a></p><p id="eec1">02Episode#<b>PySeries </b>— Python — <a href="https://readmedium.com/geogebra-plus-linear-programming-a51661c99590">Geogebra Plus Linear Programming- We’ll Create a Geogebra program to help us with our linear programming</a></p><p id="80fb">03Episode#<b>PySeries</b> — Python — Python 4 Engineer

Options

s — More Exercises! — Another Round to Make Sure that Python is Really Amazing!</p><p id="3975">04Episode#<b>PySeries</b> — Python — <a href="https://readmedium.com/linear-regressions-the-basics-1a633f351ec2">Linear Regressions — The Basics — How to Understand Linear Regression Once and For All!</a></p><p id="0910">05Episode#<b>PySeries</b> — Python — <a href="https://readmedium.com/numpy-init-python-review-f5362abbaaf9">NumPy Init & Python Review — A Crash Python Review & Initialization at NumPy lib.</a></p><p id="17c4">06Episode#<b>PySeries</b> — Python — <a href="https://readmedium.com/numpy-jupyter-notebook-1182f78ab4e1">NumPy Arrays & Jupyter Notebook — Arithmetic Operations, Indexing & Slicing, and Conditional Selection w/ np arrays</a>.</p><p id="0cc9">07Episode#<b>PySeries</b> — Python — <a href="https://readmedium.com/pandas-intro-series-970e206e2ad5">Pandas — Intro & Series — What it is? How to use it?</a></p><p id="26d6">08Episode#<b>PySeries</b> — Python — <a href="https://readmedium.com/pandas-dataframes-7ba872dcbc30">Pandas DataFrames — The primary Pandas data structure! It is a dict-like container for Series objects</a></p><p id="1687">09Episode#<b>PySeries</b> — Python —<a href="https://readmedium.com/python-4-engineers-even-more-exercises-d0141e0b06d"> Python 4 Engineers — Even More Exercises! — More Practicing Coding Questions in Python!</a></p><p id="7674">10Episode#<b>PySeries</b> — Python — Pandas — Hierarchical Index & Cross-section — Open your <a href="https://colab.research.google.com/">Colab</a> notebook and here are the follow-up exercises!</p><p id="6a67">11Episode#<b>PySeries</b> — Python — <a href="https://readmedium.com/pandas-missing-data-5142f3eda2b">Pandas — Missing Data — Let’s Continue the Python Exercises — Filling & Dropping Missing Data</a></p><p id="aa9c">12Episode#<b>PySeries</b> — Python — <a href="https://readmedium.com/pandas-group-by-3140d053b9c">Pandas — Group By — Grouping large amounts of data and compute operations on these groups</a></p><p id="91b2">13Episode#<b>PySeries</b> — Python — <a href="https://readmedium.com/pandas-merging-joining-concatenations-a35bbe1a9dd5">Pandas — Merging, Joining & Concatenations — Facilities For Easily Combining Together Series or DataFrame</a></p><p id="0af9">14Episode#<b>PySeries</b> — Python — <a href="https://readmedium.com/pandas-operations-4b8f7a4b4139">Pandas — Pandas Dataframe Examples: Column Operations</a></p><p id="b285">15Episode#<b>PySeries</b> — Python — <b>Python 4 Engineers </b>— Keeping It In The Short-Term Memory — <a href="https://readmedium.com/python-4-engineers-keeping-it-in-the-short-term-memory-4f9458016171"><b>Test Yourself!</b> Coding in Python, Again!</a></p><p id="8be8">16Episode#<b>PySeries</b> — NumPy — <a href="https://readmedium.com/numpy-review-again-f94f1c1c77e8">NumPy Review, Again;)<b> </b></a>— Python Review Free Exercises</p><p id="64e3">17Episode#<b>PySeries</b><a href="https://readmedium.com/generators-in-python-8d3de173743e">Generators in Python<b></b></a><b><a href="https://readmedium.com/numpy-review-again-f94f1c1c77e8"><b> </b></a>— Python Review Free Hints</b></p><p id="eccc">18Episode#<b>PySeries</b> — P<a href="https://readmedium.com/panda-review-again-baf0687b35de">andas Review…Again;)</a> — Python Review Free Exercise</p><p id="03bf">19Episode#<b>PySeries</b><a href="https://readmedium.com/matlibplot-seaborn-python-libs-459f6666f35f">MatlibPlot & Seaborn Python Libs </a>— Reviewing theses Plotting & Statistics Packs</p><p id="39ef">20Episode#<b>PySeries</b><a href="https://readmedium.com/seaborn-python-review-9e543b6b7a44">Seaborn Python Review</a> — Reviewing theses Plotting & Statistics Packs</p><figure id="01b6"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*j8_HbOIZwMXLFUfq.png"><figcaption></figcaption></figure></article></body>

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 names
df.index.names
FrozenList([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 
# asGroupsand 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#PySeriesGenerators in Python — Python Review Free Hints

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

19Episode#PySeriesMatlibPlot & Seaborn Python Libs — Reviewing theses Plotting & Statistics Packs

20Episode#PySeriesSeaborn Python Review — Reviewing theses Plotting & Statistics Packs

Pandas Dataframe
Python3
Pandas Multiidex
Pandas Hierarchical Index
Numpy
Recommended from ReadMedium