Pandas Tutorial | Hedaro

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 [1]:
# Import libraries
import pandas as pd
import sys
In [2]:
print('Python version ' + sys.version)
print('Pandas version: ' + pd.__version__)
Python version 3.5.1 |Anaconda custom (64-bit)| (default, Feb 16 2016, 09:49:46) [MSC v.1900 64 bit (AMD64)]
Pandas version: 0.18.1
In [3]:
# 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[3]:
one two
a 1 2
b 1 2
In [4]:
df.index
Out[4]:
Index(['a', 'b'], dtype='object')
In [5]:
# Bring the columns and place them in the index
stack = df.stack()
stack
Out[5]:
a  one    1
   two    2
b  one    1
   two    2
dtype: int64
In [6]:
# The index now includes the column names
stack.index
Out[6]:
MultiIndex(levels=[['a', 'b'], ['one', 'two']],
           labels=[[0, 0, 1, 1], [0, 1, 0, 1]])
In [7]:
unstack = df.unstack()
unstack
Out[7]:
one  a    1
     b    1
two  a    2
     b    2
dtype: int64
In [8]:
unstack.index
Out[8]:
MultiIndex(levels=[['one', 'two'], ['a', 'b']],
           labels=[[0, 0, 1, 1], [0, 1, 0, 1]])

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

In [9]:
transpose = df.T
transpose
Out[9]:
a b
one 1 1
two 2 2
In [10]:
transpose.index
Out[10]:
Index(['one', 'two'], dtype='object')

This tutorial was created by HEDARO