Pandas DataFrame pivot_table() Method


Preparation

Before any data manipulation can occur, two (2) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import numpy as np 

DataFrame pivot_table()

The pivot_table() method streamlines a DataFrame to contain only specific data (columns). For example, say we have a list of countries with associated details. We only want to display one or two columns. This method can accomplish this task.

The syntax for this method is as follows:

DataFrame.pivot_table(values=None, index=None, columns=None, aggfunc='mean', fill_value=None, margins=False, dropna=True, margins_name='All', observed=False, sort=True)
ParameterDescription
valuesThis parameter is the column to aggregate and is optional.
indexIf the parameter is an array, it must be the same length as the data. It may contain any other data types (but not a list).
columnsIf an array, it must be the same length as the data. It may contain any other data types (but not a list).
aggfuncThis parameter can be a list of functions. These name(s) will display at the top of the relevant column names (see Example 2).
fill_valueThis parameter is the value used to replace missing values in the table after the aggregation has occurred.
marginsIf set to True, this parameter will add the row/column data to create subtotal(s) or total(s). False, by default.
dropnaThis parameter will not include any columns where the value(s) are NaN. True by default.
margins_nameThis parameter is the name of the row/column containing the totals if margins parameter is True.
observedIf True, display observed values. If False, display all observed values.
sortBy default, sort is True. The values automatically sort. If False, no sort is applied.

For this example, a comma-delimited CSV file is read in. Then, a pivot table is created based on selected parameters.

Code – Example 1

df = pd.read_csv('countries.csv')
df = df.head(5)
print(df)

result = pd.pivot_table(df, values='Population', columns='Capital')
print(result)
  • Line [1] reads in a CSV file and saves to a DataFrame (df).
  • Line [2] saves the first five (5) rows of the CSV file to df (over-writing df).
  • Line [3] outputs the DataFrame to the terminal.
  • Line [4] creates a pivot table from the DataFrame based on the Population and Capital columns. The output saves to result.
  • Line [5] outputs the result to the terminal.

Output

df

 CountryCapitalPopulationArea
0GermanyBerlin   83783942 357021
1France  Paris   67081000 551695
2Spain Madrid   47431256 498511
3Italy   Rome   60317116 301338
4Poland Warsaw   38383000 312685

result

CapitalBerlinMadridParisRomeWarsaw
Population83783942 47431256 67081000 60317116 38383000

For this example, a comma-delimited CSV file is read in. A pivot table is created based on selected parameters. Notice the max function.

Code – Example 2

df = pd.read_csv('countries.csv')
df = df.head(5)

result = pd.pivot_table(df, values='Population', columns='Capital', aggfunc=[max])
print(result)
  • Line [1] reads in a comma-separated CSV file and saves to a DataFrame (df).
  • Line [2] saves the first five (5) rows of the CSV file to df (over-writing df).
  • Line [3] creates a pivot table from the DataFrame based on the Population and Capital columns. The max population is a parameter of aggfunc. The output saves to result.
  • Line [4] outputs the result to the terminal.

Output

result

 max    
CapitalBerlinMadridParisRomeWarsaw
Population83783942 47431256 67081000 60317116 38383000

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.