pandas: Reshape MultiIndexed Series#
Package Import#
import pandas as pd
import numpy as np
Dataset Import#
The dataset used in this notebook is from Kaggle - Pokemon.
data = pd.read_csv('data/Pokemon.csv')
data
Reshape a MultiIndexed Series#
data.groupby(['Type 1', 'Legendary'])['#'].count()
What if we want to convert the above Series into a DataFrame, with Type 1
as rows, Legendary
as columns, and the counts as values?
Use
unstack()
aftergroupby()
andcount()
:
data.groupby(['Type 1', 'Legendary'])['#'].count().unstack()
But there’s a better way to do this by using pivot_table()
, which can do more things like aggregation, filling missing values, and margins
, which unstack()
cannot do. See this article for more details.