Image by Author | Midjourney
Â
Let me guide you on how to use the Pandas pivot_table
function for your data summarization.
Â
Preparation
Â
Let’s start with installing the necessary packages.
pip install pandas seaborn
Â
Then, we would load the packages and the dataset example, which is Titanic.
import pandas as pd
import seaborn as sns
titanic = sns.load_dataset('titanic')
Â
Let’s move on to the next section after successfully installing the package and loading the dataset.
Â
Pivot Table with Pandas
Â
Pivot tables in Pandas allow for flexible data reorganization and analysis. Let’s examine some practical applications, starting with the simple one.
pivot = pd.pivot_table(titanic, values="age", index='class', columns="sex", aggfunc="mean")
print(pivot)
Â
Output>>>
sex female male
class
First 34.611765 41.281386
Second 28.722973 30.740707
Third 21.750000 26.507589
Â
The resulting pivot table displays average ages, with passenger classes on the vertical axis and gender categories across the top.
We can go even further with the pivot table to calculate both the mean and the sum of fares.
pivot = pd.pivot_table(titanic, values="fare", index='class', columns="sex", aggfunc=['mean', 'sum'])
print(pivot)
Â
Output>>>
mean sum
sex female male female male
class
First 106.125798 67.226127 9975.8250 8201.5875
Second 21.970121 19.741782 1669.7292 2132.1125
Third 16.118810 12.661633 2321.1086 4393.5865
Â
We can create our function. For example, we create a function that takes the data maximum and minimum values differences and divides them by two.
def data_div_two(x):
return (x.max() - x.min())/2
pivot = pd.pivot_table(titanic, values="age", index='class', columns="sex", aggfunc=data_div_two)
print(pivot)
Â
Output>>>
sex female male
class
First 30.500 39.540
Second 27.500 34.665
Third 31.125 36.790
Â
Lastly, you can add the margins to see the differences between the overall grouping average and the specific sub-group.
pivot = pd.pivot_table(titanic, values="age", index='class', columns="sex", aggfunc="mean", margins=True)
print(pivot)
Â
Output>>>
sex female male All
class
First 34.611765 41.281386 38.233441
Second 28.722973 30.740707 29.877630
Third 21.750000 26.507589 25.140620
All 27.915709 30.726645 29.699118
Â
Mastering the pivot_table
function would allow you to get insight from your dataset.
Â
Additional Resources
Â
Â
Â
Cornellius Yudha Wijaya is a data science assistant manager and data writer. While working full-time at Allianz Indonesia, he loves to share Python and data tips via social media and writing media. Cornellius writes on a variety of AI and machine learning topics.