Pandas is a powerful data manipulation tool widely used by data scientists and analysts worldwide. One of the most common tasks that analysts perform is data aggregation, which involves summarizing data based on specific criteria.

Pandas offers a variety of functions to aggregate data, including pivot tables. A pivot table is a powerful tool for summarizing data in a tabular format.

It allows analysts to extract insights from large datasets by summarizing columns based on different criteria. Pivot tables enable analysts to create more complex analyses than is possible with basic statistics.

In this article, we will discuss how to create pivot tables in Pandas and how to add filters to make the data more meaningful.

## Creating a Pivot Table in Pandas

Creating a pivot table in Pandas is quite simple. We first need to import the Pandas library into our Python environment.

Once we have imported the library, we can create a Pandas DataFrame containing our data. Let’s use a basketball player dataset as an example.

We start by importing the dataset into our Python environment and transforming it into a Pandas DataFrame.

```
import pandas as pd
basketball_data = {'Season': ['2017-18', '2018-19', '2019-20', '2017-18', '2018-19', '2019-20'],
'Player': ['LeBron James', 'LeBron James', 'LeBron James', 'Kevin Durant', 'Kevin Durant', 'Kevin Durant'],
'PPG': [27.4, 27.4, 25.3, 26.4, 26.0, 26.4],
'RPG': [8.5, 8.5, 7.9, 6.8, 6.4, 7.1],
'APG': [9.1, 8.3, 10.2, 5.4, 5.9, 5.6]}
basketball_df = pd.DataFrame(basketball_data)
```

Once we have created the DataFrame, we can create a pivot table using the `pivot_table()`

method in Pandas. For example, if we wanted to create a pivot table that summarizes the average points per game (PPG) by player and season, we can do the following:

```
pivot_table = basketball_df.pivot_table(index=['Player', 'Season'],
values=['PPG'],
aggfunc='sum')
```

Here, we have specified the `Player`

and `Season`

columns as the index columns, `PPG`

as the value to be summarized, and `'sum'`

as the aggregation function.

This will give us a pivot table that summarizes the total PPG for each player by season.

## Adding Filters to Pandas Pivot Table

Filters can be added to pivot tables to extract more specific insights from the data. We can apply filters to the pivot table by adding a filtering criterion using the `query()`

method in Pandas.

Let’s extend our earlier example by filtering the data to include only the seasons `2018-19`

and `2019-20`

. We do this by adding the following line of code:

```
pivot_table = pivot_table.query("Season == ['2018-19', '2019-20']")
```

Here, we have added a filtering criterion that only includes data for seasons `'2018-19'`

and `'2019-20'`

.

When we print the resulting pivot table, we will only see data for those seasons.

```
PPG
Player Season
Kevin Durant 2018-19 52.400
2019-20 52.800
LeBron James 2018-19 54.800
2019-20 46.700
```

We can also apply more complex filters to the pivot table. For example, we can filter the data to include only players who scored more than 26 PPG.

```
pivot_table = pivot_table.query("PPG > 26")
```

When we print the resulting pivot table, we will only see data for players who scored more than 26 PPG.

```
PPG
Player Season
Kevin Durant 2018-19 26.000
2019-20 26.400
LeBron James 2017-18 27.400
2018-19 27.400
```

To make the results more readable, we can sort the pivot table by PPG.

```
pivot_table = pivot_table.sort_values(['PPG'], ascending=False)
```

When we print the resulting pivot table, we will see the data sorted by PPG.

```
PPG
Player Season
LeBron James 2017-18 27.400
2018-19 27.400
Kevin Durant 2019-20 26.400
2018-19 26.000
```

## Conclusion

Pandas is a versatile data analysis tool that allows analysts to extract insights from large datasets. Pivot tables are a powerful tool that enables analysts to summarize data based on specific criteria.

We have seen how to create pivot tables in Pandas and how to add filters that make the data more meaningful. When applied correctly, pivot tables can help analysts uncover hidden patterns and trends in the data. Pivot tables are a powerful tool in Pandas that allow analysts to summarize data based on specific criteria.

In addition to filtering the data, analysts can also group and aggregate data in a pivot table. Grouping data in a pivot table can help to identify patterns and relationships that may not be immediately apparent.

Aggregating data can help to summarize various aspects of the data such as mean, min, max, and more. In this article, we will discuss how to group and aggregate data in a Pandas pivot table.

## Grouping Data in Pandas Pivot Table

Grouping data in a pivot table is an essential step in analyzing large datasets. It enables grouping the data in a way that makes sense and is more easily understandable.

To group data, we use the `groupby()`

method in Pandas. This method allows us to group data by particular columns in our dataset.

Let’s continue with our basketball player dataset from the previous sections. In this dataset, we have the following columns: `'Season'`

, `'Player'`

, `'PPG'`

, `'RPG'`

, and `'APG'`

.

Suppose we are interested in grouping the data by the `'Season'`

column. We can do so using the following code:

```
grouped_data = basketball_df.groupby(['Season'])
```

This code creates a new object, `grouped_data`

, which represents the data grouped by the `'Season'`

column.

Using the `size()`

method, we can see how many observations are contained within each group:

```
grouped_data.size()
Season
2017-18 2
2018-19 2
2019-20 2
dtype: int64
```

In this example, we see that each season has two observations in our dataset.

## Aggregating Data in Pandas Pivot Table

Aggregating data in a pivot table can help analyze the dataset more clearly. It involves summarizing data based on a particular criterion or function.

In Pandas, we can use the `agg()`

method to aggregate data in a pivot table. Let’s use our basketball player dataset as an example.

We can create a pivot table to summarize and aggregate data by season. We can use the following code to create the pivot table:

```
pivot_table = basketball_df.pivot_table(index='Season',
values=['PPG', 'RPG', 'APG'],
aggfunc='mean')
```

Here, we have specified the index as the `'Season'`

column and the values to be summarized as the `PPG`

, `RPG`

, and `APG`

columns.

We have used the `'mean'`

aggregation function to calculate the mean value for each column by season. When we print the resulting pivot table, we will see the averages for these statistics grouped by the different seasons:

```
APG PPG RPG
Season
2017-18 7.25 26.900000 7.650000
2018-19 7.40 26.266667 7.133333
2019-20 7.300 26.033333 7.000000
```

In this example, we can see that LeBron James had the highest PPG during the 2017-18 season, with an average of 27 points per game. So, grouping and aggregating data using pivot tables can be helpful in highlighting patterns, relationships, and trends in large datasets.

We can also use the `agg()`

method to calculate other aggregate statistics such as min, max, and sum. Let’s see an example of how to do this with the same basketball player dataset.

```
pivot_table = basketball_df.pivot_table(index='Season',
values=['PPG', 'RPG', 'APG'],
aggfunc=['min', 'max', 'mean'])
```

Here we have used the `agg()`

method to calculate mean, min, and max values for the `PPG`

, `RPG`

, and `APG`

columns by season. The resulting pivot table shows the mean, min, and max values grouped with respect to the season:

```
min max mean
APG PPG RPG APG PPG RPG APG PPG RPG
Season
2017-18 5 26.4 6.8 9.1 27.4 8.5 7.250000 26.900000 7.650000
2018-19 5 26.0 6.4 8.3 27.4 8.5 7.400000 26.266667 7.133333
2019-20 5 25.3 7.1 10.2 26.4 7.9 7.300000 26.033333 7.000000
```

Here, we can see the minimum and maximum value and mean for the `'APG'`

, `'PPG'`

, and `'RPG'`

columns grouped with respect to the season.

## Conclusion

Using pivot tables in Pandas, analysts can easily group and aggregate data by different criteria. Grouping data in a pivot table can help identify patterns and relationships, while aggregating data can help summarize various aspects such as mean, min, max, and more.

With the help of Pivot Table, analyzing large datasets becomes simpler, and the insights extracted from it can help make informed decisions. Pandas Pivot Tables are highly versatile, efficient, and straightforward, making them an essential part of every Data Analyst and Scientist’s toolkit. Pandas offers a powerful tool, the pivot table, that enables data analysts to analyze and visualize complex data relationships quickly.

Pivot tables group, aggregate, and reshape data to highlight relationships and uncover insights by summarizing large data sets in a visual format. In this article, we will discuss how to reshape data in Pandas pivot tables.

## Reshaping Data in Pandas Pivot Table

When working with large datasets, reshaping or restructuring data can make it easier to analyze with pivot tables. In pandas, we can use the following methods to reshape data in a pivot table: `melt()`

, `unstack()`

, and `stack()`

.

Let’s use our basketball player dataset to illustrate how to reshape data using these methods. Suppose we want to create a pivot table that shows the average PPG of individual players in each season.

We start by importing the Pandas library and loading the basketball player dataset.

```
import pandas as pd
basketball_data = {'Season': ['2017-18', '2018-19', '2019-20', '2017-18', '2018-19', '2019-20'],
'Player': ['LeBron James', 'LeBron James', 'LeBron James', 'Kevin Durant', 'Kevin Durant', 'Kevin Durant'],
'PPG': [27.4, 27.4, 25.3, 26.4, 26.0, 26.4],
'RPG': [8.5, 8.5, 7.9, 6.8, 6.4, 7.1],
'APG': [9.1, 8.3, 10.2, 5.4, 5.9, 5.6]}
basketball_df = pd.DataFrame(basketball_data)
```

We can use the `pivot_table()`

method to create a pivot table that shows the average PPG by player and season.

```
pivot_table = basketball_df.pivot_table(index='Player', columns='Season', values='PPG', aggfunc='mean')
```

### The resulting pivot table will show the average PPG of each player by season:

```
Season 2017-18 2018-19 2019-20
Player
Kevin Durant 26.4 26.0 26.4
LeBron James 27.4 27.4 25.3
```

This pivot table is useful, but it has a hierarchical structure that can be challenging to work with. Reshaping the data can make it easier to work with groups of data or visualize trends.

### Using the `melt()`

Method to Reshape Pivot Table Data

The `melt()`

method in pandas converts a wide-format DataFrame to a long-format DataFrame. A wide-format DataFrame is a table with multiple columns representing the same data.

In contrast, a long-format DataFrame displays multiple ‘variable’ columns with data-dependent ‘value’ entries for each variable. In a pivot table, a wide-format DataFrame represents the aggregated data, while a long-format DataFrame represents the raw data.

To reshape the pivot table with the `melt()`

method, we specify the `id_vars`

parameter to define columns we want to keep fixed. Then, we can melt the pivot table, as follows:

```
melted = pivot_table.reset_index().melt(id_vars=['Player'], var_name='Season')
```

In this code, we use the `reset_index()`

method to extract the row indexes, and we define the `Player`

column as the `id_vars`

.

The `var_name`

argument specifies the name of the new variable column to add. The resulting data will look like this:

```
Player Season value
0 Kevin Durant 2017-18 26.4
1 LeBron James 2017-18 27.4
2 Kevin Durant 2018-19 26.0
3 LeBron James 2018-19 27.4
4 Kevin Durant 2019-20 26.4
5 LeBron James 2019-20 25.3
```

Now we have a table with three columns: `Player`

, `Season`

, and `value`

.

We can use this table to perform any further analyses in pandas.

### Using the `unstack()`

and `stack()`

Methods to Reshape Data in a Pivot Table

The `unstack()`

method in Pandas allows us to rotate a dataset from long to wide format, making it more comfortable to view and analyze pivot tables.

We can use `unstack()`

after creating a pivot table. We can also use the `stack()`

method to reverse the operation by going from a wide-format DataFrame to a long-format DataFrame.

```
wide_format_df = pivot_table.unstack()
```

Here, we have used the `unstack()`

method to rotate the pivot table and reshape data in the wide format. The resulting `wide_format_df`

DataFrame is shown below:

```
Player
Season 2017-18 2018-19 2019-20
PPG Kevin Durant 26.4 26.0 26.4
LeBron James 27.4 27.4 25.3
```

We can then use the `stack()`

method to reshape the data back to long-format:

```
long_format_df = wide_format_df.stack().reset_index(name='PPG')
```

In this code, the `reset_index()`

method is used to assign a column name to the reshaped data.