 # Lesson 5¶

These tutorials are also available through an email course, please visit http://www.hedaro.com/pandas-tutorial to sign up today.

We will be taking a brief look at the stack and unstack functions.

In :
# Import libraries
import pandas as pd
import sys

In :
print('Python version ' + sys.version)
print('Pandas version: ' + pd.__version__)

Python version 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]
Pandas version: 1.0.5

In :
# Our small data set
d = {'one':[1,1],'two':[2,2]}
i = ['a','b']

# Create dataframe
df = pd.DataFrame(data = d, index = i)
df

Out:
one two
a 1 2
b 1 2
In :
df.index

Out:
Index(['a', 'b'], dtype='object')
In :
# Bring the columns and place them in the index
stack = df.stack()
stack

Out:
a  one    1
two    2
b  one    1
two    2
dtype: int64
In :
# The index now includes the column names
stack.index

Out:
MultiIndex([('a', 'one'),
('a', 'two'),
('b', 'one'),
('b', 'two')],
)
In :
unstack = df.unstack()
unstack

Out:
one  a    1
b    1
two  a    2
b    2
dtype: int64
In :
unstack.index

Out:
MultiIndex([('one', 'a'),
('one', 'b'),
('two', 'a'),
('two', 'b')],
)

We can also flip the column names with the index using the T (transpose) function.

In :
transpose = df.T
transpose

Out:
a b
one 1 1
two 2 2
In :
transpose.index

Out:
Index(['one', 'two'], dtype='object')

This tutorial was created by HEDARO