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)
Parameter | Description |
---|---|
values | This parameter is the column to aggregate and is optional. |
index | If 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). |
columns | If an array, it must be the same length as the data. It may contain any other data types (but not a list). |
aggfunc | This parameter can be a list of functions. These name(s) will display at the top of the relevant column names (see Example 2). |
fill_value | This parameter is the value used to replace missing values in the table after the aggregation has occurred. |
margins | If set to True , this parameter will add the row/column data to create subtotal(s) or total(s). False , by default. |
dropna | This parameter will not include any columns where the value(s) are NaN . True by default. |
margins_name | This parameter is the name of the row/column containing the totals if margins parameter is True . |
observed | If True , display observed values. If False , display all observed values. |
sort | By 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-writingdf
). - 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
Country | Capital | Population | Area | |
0 | Germany | Berlin | 83783942 | 357021 |
1 | France | Paris | 67081000 | 551695 |
2 | Spain | Madrid | 47431256 | 498511 |
3 | Italy | Rome | 60317116 | 301338 |
4 | Poland | Warsaw | 38383000 | 312685 |
result
Capital | Berlin | Madrid | Paris | Rome | Warsaw |
Population | 83783942 | 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-writingdf
). - 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 toresult
. - Line [4] outputs the result to the terminal.
Output
result
max | |||||
Capital | Berlin | Madrid | Paris | Rome | Warsaw |
Population | 83783942 | 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.