Lesson 4

*Pivot tables* are powerful tools that allow us to summarize, analyze, and explore data in different ways. They are commonly used in data analysis for generating insights from data by arranging, sorting, and aggregating it.

In this lesson, we will learn how to create and use pivot tables in `Pandas`

. By the end of this lesson, you will understand the basics of pivot tables, how to create them, and how they can help you extract valuable insights from your data.

A `pivot table`

allows us to summarize data by grouping it in a way that makes it easier to extract meaningful insights. Think of it like organizing toys in a toy store. Instead of having all toys mixed up, you sort them by category and then further by different attributes. Pivot tables help us do something similar with our data.

The `Pandas`

library provides a function called `pivot_table()`

that makes creating pivot tables in Python straightforward. One reason pivot tables are so useful is that they allow us to easily perform aggregate functions like mean, sum, and count on data.

Here are the important parameters of the `pivot_table()`

function:

`index`

: The column(s) to group by, like aisles in a store.`columns`

: The column whose distinct values will form the columns of the pivot table.`values`

: The columns containing the data you want to aggregate, like toy prices.`aggfunc`

: The function used to aggregate the data (e.g., mean or sum).

Let's start by creating a simple pivot table. Suppose you have data about different products, and you want to see the average price of each product category:

Python`1import pandas as pd 2 3# Sample DataFrame 4data = { 5 'Product': ['Toy', 'Toy', 'Book', 'Book', 'Electronic'], 6 'Category': ['A', 'B', 'A', 'B', 'A'], 7 'Price': [10, 15, 7, 12, 100] 8} 9df = pd.DataFrame(data) 10 11# Create a pivot table 12pivot_table = df.pivot_table(index='Product', values='Price', aggfunc='mean') 13print(pivot_table)`

The output will be:

`1 Price 2Product 3Book 9.5 4Electronic 100.0 5Toy 12.5`

This code groups the data by the `Product`

column and calculates the average price for each product. Running this shows the average price for `Toy`

, `Book`

, and `Electronic`

.

Let's consider a complex example. We will still use the Titanic dataset but focus on more dimensions. We will analyze the average fare and survival rate based on the class of the passengers and their gender.

Python`1import seaborn as sns 2 3# Load Titanic dataset using seaborn 4titanic = sns.load_dataset('titanic') 5 6# Create a simplified pivot table 7pivot = titanic.pivot_table(index='class', columns='sex', values=['survived', 'fare'], aggfunc='mean') 8print(pivot)`

The output will be:

`1 fare survived 2sex female male female male 3class 4First 106.125798 67.226127 0.968085 0.368852 5Second 21.970121 19.741782 0.921053 0.157407 6Third 16.118810 12.661633 0.500000 0.135447`

This pivot table shows the average fare and survival rate for passengers of different classes and genders.

Let's break down the arguments:

**Index**: The rows of the table (indexed by`class`

).**Columns**: The columns (grouped by`sex`

).**Values**: Data being summarized (`fare`

and`survived`

).

Here is how to interpret the result:

- The
`class`

index groups the data by the passenger class (First, Second, Third). - The
`sex`

columns categorize data by gender (female, male). - Under
`fare`

, we can see the average fare paid by female and male passengers in each class. - Under
`survived`

, we see the average survival rate for female and male passengers in each class.

Let's consider insights we could find using this table.

- The average fare for female passengers in the First Class is 106.13, and for male – 67.23.
- The survival rate for female passengers in the First Class is significantly higher than for male, it is 96.81% vs 36.89%.
- In the Third Class, only 50% of female passengers survived

By breaking the data down this way, you can uncover meaningful insights and trends that may not be immediately obvious in a more granular or ungrouped dataset.

Great job! We've covered a fundamental aspect of data analysis: pivot tables. Let's recap what we've learned:

- What pivot tables are and why they are useful.
- How to create a basic pivot table using
`Pandas`

. - How to create a more complex pivot table with multiple dimensions and aggregate functions.
- How to interpret the data from a pivot table.

Using pivot tables, you can summarize and analyze large amounts of data efficiently, helping you draw meaningful insights with ease.

Now it's time to put what you've learned into practice! In the upcoming practice session, you will get hands-on experience creating and working with pivot tables. This will solidify your understanding and prepare you for more advanced data analysis tasks. Let's dive in and start practicing with pivot tables!