Search⌘ K
AI Features

Pandas DataFrame Operations - Pivot Tables and Functions

Explore how to use Pandas pivot tables to summarize multidimensional data and apply custom functions for data classification. This lesson helps you understand efficient data manipulation techniques in Python using Pandas for insightful analysis without inefficient iteration.

11. Pivot Table

We have seen how grouping lets us explore relationships within a dataset. A pivot table is a similar operation. You have probably encountered it in spreadsheets or some other programs that operate on tables. If you have ever worked with Excel, Pandas can be used to create Excel style pivot tables.

The pivot table takes simple column-wise data as input, and groups the entries into a two-dimensional table to give a multidimensional summary of the data. Hard to understand? Let’s understand the concept with an example!

Say we want to compare the $$$ earned by the various directors per year. We can create a pivot table using pivot_table; we can set index =‘Director’ (row of the pivot table) and get the yearly revenue information by setting columns = ‘Year’:

Python 3.5
# Let's calculate the mean revenue per director but by using a pivot table instead of groupby as seen previously
movies_df_title_indexed.pivot_table('Revenue (Millions)', index='Director',
aggfunc='sum', columns='Year').head()

The aggfunc parameter ...