How to create Pivot Table of Dataframe in Python?

Pandas DataFrame.pivot_table()

The Pandas pivot_table() is used to calculate, aggregate, and summarize your data. It is defined as a powerful tool that aggregates data with calculations such as Sum, Count, Average, Max, and Min .

It also allows the user to sort and filter your data when the pivot table has been created.

Parameters:

  • data: A DataFrame.
  • values: It is an optional parameter and refers the column to aggregate.
  • index: It refers to the column, Grouper, and array.

If we pass an array, it must be of the same length as data.

  • columns: Refers to column, Grouper, and array

If we pass an array, it must be of the same length as data.

  • aggfunc: function, list of functions, dict, default numpy.mean
    If we pass the list of functions, the resulting pivot table will have hierarchical columns whose top level are the function names.
    If we pass a dict, the key is referred to as a column to aggregate, and value is function or list of functions.
  • fill_value[scalar, default None]: It replaces the missing values with a value.
  • margins[boolean, default False]: It add all the row / columns (e.g. for subtotal / grand totals)
  • dropna[boolean, default True] : It drops the columns whose entries are all NaN.
  • margins_name[string, default ‘All’] : It refers to the name of the row/column that will contain the totals when margins are True.

Returns:

It returns a DataFrame as the output.

Example:

# importing pandas as pd   
import pandas as pd   
import numpy as np   
     
# create dataframe   
info = pd.DataFrame({'P': ['Smith', 'John', 'William', 'Parker'],   
      'Q': ['Python', 'C', 'C++', 'Java'],   
      'R': [19, 24, 22, 25]})   
info   
table = pd.pivot_table(info, index =['P', 'Q'])     
table  

Output

   P          Q         R
   John       C         24
   Parker     Java      25
   Smith      Python    19
   William    C         22