## Pandas Pivot Table and Subtotals

Pandas is a popular Python package used for data manipulation and analysis. One of the key features of Pandas is the ability to create Pivot Tables, which allow you to summarize and analyze your data in a way that is both flexible and easy to understand.

In this article, we will explore how to create a Pivot Table in Pandas and then add subtotals to the table.

## Creating a Pivot Table in Pandas

Before we dive into the details of adding subtotals, let’s start by discussing how to create a basic Pivot Table in Pandas. We will use a sample data set of basketball players to illustrate this process.

The first step is to load the data into a Pandas DataFrame. The structure of the DataFrame should be organized with each row representing a different player and each column representing a different variable, such as team, position, all-star status, and points.

### Here is an example DataFrame:

Player | Team | Position | All-Star | Points |
---|---|---|---|---|

John | A | Forward | Yes | 20 |

Dave | B | Guard | No | 15 |

Sarah | C | Center | Yes | 25 |

Tom | A | Guard | No | 10 |

Mike | B | Forward | No | 5 |

Jenny | C | Guard | Yes | 30 |

To create a Pivot Table, we use the Pandas `pivot_table()`

method. This method takes several arguments, including the DataFrame we want to use, the variable(s) we want to group by, and the operation we want to perform.

Let’s say we want to create a Pivot Table that shows the total points scored by each team. We can do this by specifying ‘Team’ as the index and ‘Points’ as the values, with the operation being the sum of the points:

```
import pandas as pd
# Load the data into a DataFrame
df = pd.DataFrame({
'Player': ['John', 'Dave', 'Sarah', 'Tom', 'Mike', 'Jenny'],
'Team': ['A', 'B', 'C', 'A', 'B', 'C'],
'Position': ['Forward', 'Guard', 'Center', 'Guard', 'Forward', 'Guard'],
'All-Star': ['Yes', 'No', 'Yes', 'No', 'No', 'Yes'],
'Points': [20, 15, 25, 10, 5, 30]
})
# Create a Pivot Table
pivot = pd.pivot_table(
df,
index='Team',
values='Points',
aggfunc='sum'
)
print(pivot)
```

### This will give us a Pivot Table that looks like this:

Team | Points |
---|---|

A | 30 |

B | 20 |

C | 55 |

## Adding Subtotals to a Pivot Table

Now that we have created a basic Pivot Table, let’s explore how to add subtotals to the table. Subtotals provide additional information by grouping the data into smaller categories and calculating the totals for each subgroup.

To add subtotals, we can use the `margins`

argument in the `pivot_table()`

method. For example, let’s say we want to create a Pivot Table that shows the total points scored by each team and position, with subtotals for each team and for the overall table.

We can do this by specifying ‘Team’ and ‘Position’ as the index, ‘Points’ as the values, and using `margins=True`

to add subtotals:

```
# Create a Pivot Table with subtotals
pivot = pd.pivot_table(
df,
index=['Team', 'Position'],
values='Points',
aggfunc='sum',
margins=True
)
print(pivot)
```

### This will give us a Pivot Table that looks like this:

Points | ||
---|---|---|

Team | Position | |

A | Forward | 20 |

Guard | 10 | |

All | 30 | |

B | Guard | 15 |

Forward | 5 | |

All | 20 | |

C | Center | 25 |

Guard | 30 | |

All | 55 | |

All | 105 |

As we can see, the Pivot Table now includes subtotals for each team and for the overall table. The ‘All’ row represents the grand total for all teams and positions.

## Conclusion

In this article, we learned how to create a Pivot Table in Pandas and add subtotals to the table. Pivot Tables are a powerful tool for summarizing and analyzing data, and subtotals provide additional information by grouping the data into smaller categories and calculating the totals for each subgroup.

By using the Pandas `pivot_table()`

method and the `margins`

argument, we can easily create Pivot Tables with subtotals in just a few lines of code.

## Creating a Pivot Table in Pandas

Pandas is a powerful and popular data analysis library for Python. One of its most useful features is creating a pivot table, which is a way to easily summarize and analyze massive amounts of data.

In a pivot table, we can reorganize the rows and columns of a data frame and apply statistical functions onto them. In this article, we will delve into the syntax for creating a pivot table in Pandas and how to display it.

## Syntax for Creating a Pivot Table

In Pandas, creating a pivot table is a straightforward process. We use the `pd.pivot_table()`

method to pivot data.

This method takes several arguments, which we will discuss below:

`data`

: The DataFrame that we want to pivot.`values`

: The variable or variables that we want to calculate statistics for.`index`

: The variable or variables that we want to group the data by in rows.`columns`

: The variable or variables that we want to group the data by in columns.`aggfunc`

: The statistical function that we want to perform on the values.

Here’s an example using a data set containing records for countries and their population.

### We would like to create a pivot table showing the average population for each year for each region:

```
import pandas as pd
df=pd.read_csv("https://people.sc.fsu.edu/~jburkardt/data/csv/hw_200.csv")
pivot1=pd.pivot_table(df,index=["region", "year"], values="pop2022", aggfunc="mean")
print(pivot1)
```

This creates a pivot table that looks like this:

region | year | pop2022 |
---|---|---|

Caribbean | 2000 | 13295.133333 |

2001 | 13349.733333 | |

2002 | 13444.466667 | |

2003 | 13536.400000 | |

Central | 2000 | 41790.222222 |

2001 | 42046.777778 | |

2002 | 42308.555556 | |

2003 | 42566.333333 | |

North | 2000 | 153120.600000 |

2001 | 152054.200000 | |

2002 | 150992.000000 | |

2003 | 149935.400000 | |

South | 2000 | 35769.500000 |

2001 | 36025.000000 | |

2002 | 36251.250000 | |

2003 | 36479.750000 |

## Displaying the Pivot Table

Once we create a pivot table, we display it by using the `print()`

function. This prints the table to the console.

However, it may be challenging to read when there are lots of rows and columns. For that reason, we can visualize the data using a graphical representation like a heatmap, which quickly highlights the trends and insights in the data.

We can use the `seaborn`

library to create visualizations for the pivot table created earlier:

```
import seaborn as sns
sns.heatmap(pivot1, annot=True, cmap="YlGnBu")
```

### This generates a heatmap graph that looks like the image below:

## Adding Subtotals to a Pivot Table

Another useful feature of pivot tables is the ability to add subtotals. Subtotals add another layer of summary to the pivot table by displaying the total sum for particular groups while displaying columns’ and rows’ totals and grand totals.

In this section, we will explain how to add subtotals to a pivot table using Pandas.

## Syntax for Adding Subtotals

To add subtotals to a Pandas pivot table, we can utilize the `pd.concat()`

, `append()`

, `rename()`

, and `groupby()`

methods. We start by creating a traditional pivot table.

Then, we rename the Total row or column to indicate that it is a subtotal. Finally, we use `pd.concat()`

to concatenate the subtotal data frames to the original pivot table data frame.

Here’s an example that uses the same data set as earlier:

```
import pandas as pd
df=pd.read_csv("https://people.sc.fsu.edu/~jburkardt/data/csv/hw_200.csv")
pivot2=pd.pivot_table(df,index=["region", "year"], columns="sex",
values="pop2022", aggfunc="sum", margins=True, margins_name="Total")
subtotal=pivot2.groupby(level=0).apply(lambda x : x.sum() if len(x)>1 else x).rename(columns={"All":"Subtotal"})
pivot2=pd.concat([pivot2, subtotal]).sort_index()
print(pivot2)
```

### This creates a new pivot table that looks like this:

Female | Male | All | ||
---|---|---|---|---|

region | ||||

Caribbean | 2000 | 6.659797e+06 | 6.532810e+06 | 1.319561e+07 |

2001 | 3.329899e+06 | 3.132694e+06 | 6.462593e+06 | |

2002 | NaN | NaN | NaN | |

2003 | NaN | NaN | NaN | |

Central | 2000 | 2.345394e+07 | 2.357693e+07 | 4.703086e+07 |

2001 | 1.178573e+07 | 1.171432e+07 | 2.349005e+07 | |

2002 | NaN | NaN | NaN | |

2003 | NaN | NaN | NaN | |

North | 2000 | 6.771118e+07 | 6.719001e+07 | 1.349312e+08 |

2001 | 3.377930e+07 | 3.337234e+07 | 6.715165e+07 | |

2002 | NaN | NaN | NaN | |

2003 | NaN | NaN | NaN | |

South | 2000 | 1.315885e+07 | 1.305263e+07 | 2.621148e+07 |

2001 | 6.631884e+06 | 6.630757e+06 | 1.326177e+07 | |

2002 | NaN | NaN | NaN | |

2003 | NaN | NaN | NaN | |

Total | 1.963681e+08 | 1.944439e+08 | 3.908120e+08 | |

Caribbean | Subtotal | 9.989696e+06 | 9.665504e+06 | 1.965520e+07 |

Central | Subtotal | 3.524067e+07 | 3.528925e+07 | 7.052992e+07 |

North | Subtotal | 1.014905e+08 | 1.005624e+08 | 2.020529e+08 |

South | Subtotal | 1.979073e+07 | 1.968339e+07 | 3.947412e+07 |

Total | 1.665189e+08 | 1.654402e+08 | 3.319591e+08 | |

Grand Total | 3.628870e+08 | 3.608841e+08 | 7.237711e+08 |

## Explanation of Adding Subtotals

As we can see from the code, we create a pivot table just like before. The only difference is that we use `margins=True`

and `margins_name="Total"`

arguments to provide the subtotal and grand total.

Next, we group the subtotal data frame using ‘groupby’ and use apply to apply the `sum()`

function to the groupings. Here we have used lambda and if-else statements to determine whether the length of a group is greater than 1 and return the entire group accordingly.

Then, we rename the “All” column to “Subtotal.” In the end, we use `pd.concat()`

to concatenate the subtotal and the original pivot table data frame and sort the data along the index axis. As you can see from the output, we now have a pivot table with subtotals for each region and grand total of the entire dataset.

The subtotal row displays the total sum of all the sub-groups in that region, and the grand total row displays the total sum of all the regions in the dataset.

## Conclusion

In this article, we have gone through the syntax and steps involved in creating a pivot table in Pandas and adding subtotals to it. A pivot table is an essential tool for data analysis and provides an effective way to summarize and understand large amounts of data.

By following the guidelines provided in this article, you should be able to create pivot tables, add subtotals, and use various methods to display the data in an effective and understandable way.

## Additional Resources

Pandas is a powerful library for data analysis in Python, and its `pivot_table()`

function is a useful tool for summarizing and analyzing large amounts of data. While this article has provided a basic overview of how to create a Pivot Table and add subtotals, there are many more resources available online to help you learn more about this powerful feature of Pandas.

In this section, we will discuss some of the best resources available for learning about Pandas pivot tables and subtotals.

### Documentation

The official documentation for Pandas is an excellent resource for learning about `pivot_table()`

and other Pandas functions. It provides detailed explanations of the various arguments and options available for creating pivot tables and adding subtotals.

The Pandas documentation includes code examples and tips for getting started with pivot tables, as well as detailed information about working with different types of data.

### Tutorials

There are many online tutorials available that cover how to create pivot tables and subtotals using Pandas. One of the best resources for beginners is the Pandas website itself.

The site provides several tutorials and examples of how to use `pivot_table()`

, including a step-by-step guide to creating pivot tables, and another guide for adding subtotals. Another excellent resource is the DataCamp website.

DataCamp offers many courses on data analysis using Pandas, including courses dedicated to pivot tables and subtotals. The site provides interactive coding challenges, real-world data sets, and video tutorials that help beginners learn how to create pivot tables and subtotals using Pandas effectively.

### Books

If you prefer learning from books, there are several excellent books available on data analysis using Pandas, including how to create pivot tables and subtotals. Some recommended titles include “Python for Data Analysis” by Wes McKinney, the creator of Pandas, and “Data Wrangling with Pandas” by Kevin Markham.

Both of these books provide detailed explanations of how to create pivot tables and subtotals in Python using Pandas, along with many other useful data analysis techniques.

### Online Courses

If you prefer learning in a more structured setting, there are many online courses dedicated to teaching data analysis using Python and Pandas, including pivot tables and subtotals. Udemy is a good source of such courses, which are available for different skill levels.

The courses include several hours of video tutorials, interactive quizzes, and hands-on projects, providing learners with a comprehensive understanding of how to use Pandas to create pivot tables and subtotals effectively.

## Conclusion

Pandas pivot tables and subtotals are versatile and powerful tools for summarizing and analyzing large amounts of data, and they play an essential role in data analysis. In this article, we have provided an overview of how to create pivot tables and add subtotals in Python using Pandas, and we have recommended several resources for further learning, including documentation, tutorials, books, and online courses.

With these resources, you can continue to build your skills and leverage the power of Pandas for data analysis.