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.
# Import libraries
import pandas as pd
import sys
print('Python version ' + sys.version)
print('Pandas version: ' + pd.__version__)
# Our small data set
d = {'one':[1,1],'two':[2,2]}
i = ['a','b']
# Create dataframe
df = pd.DataFrame(data = d, index = i)
df
df.index
# Bring the columns and place them in the index
stack = df.stack()
stack
# The index now includes the column names
stack.index
unstack = df.unstack()
unstack
unstack.index
We can also flip the column names with the index using the T (transpose) function.
transpose = df.T
transpose
transpose.index
This tutorial was created by HEDARO