Skip to content
Related Articles

Related Articles

Improve Article

Pivot Tables in Pandas

  • Last Updated : 11 Dec, 2020

In this article, we will see the Pivot Tables in Pandas. Let’s discuss some concepts:

Pandas : Pandas is an open-source library that is built on top of the NumPy library. It is a Python package that offers various data structures and operations for manipulating numerical data and time series. It is mainly popular for importing and analyzing data much easier. Pandas is fast and it has high-performance & productivity for users.

Pivot Tables: A pivot table is a table of statistics that summarizes the data of a more extensive table (such as from a database, spreadsheet, or business intelligence program). This summary might include sums, averages, or other statistics, which the pivot table groups together in a meaningful way.

Steps Needed

  • Import Library (Pandas)
  • Import / Load / Create data.
  • Use Pandas.pivot_table() method with different variants.

Here, we will discuss some variants of pivot table over the dataframe shown below :

Python3






# import packages
import pandas as pd
  
# create data
df = pd.DataFrame({'ID': {0: 23, 1: 43, 2: 12
                          3: 13, 4: 67, 5: 89,
                          6: 90, 7: 56, 8: 34},
                     
                 'Name': {0: 'Ram', 1: 'Deep', 2: 'Yash',
                          3: 'Aman', 4: 'Arjun', 5: 'Aditya',
                          6: 'Akash', 7: 'Chalsea',
                          8: 'Divya'},
                     
                 'Marks': {0: 89, 1: 97, 2: 45,
                           3: 78, 4: 56, 5: 76,
                           6: 81, 7: 87, 8: 100},
                     
                 'Grade': {0: 'B', 1: 'A', 2: 'F',
                           3: 'C', 4: 'E', 5: 'C',
                           6: 'B', 7: 'B', 8: 'A'}})
  
# view data
display(df)

Output:

Example 1: Simple use of pivot_table() method.

Python3




# import packages
import pandas as pd
  
# create data
df = pd.DataFrame({'ID': {0: 23, 1: 43, 2: 12
                          3: 13, 4: 67, 5: 89,
                          6: 90, 7: 56, 8: 34},
                     
                 'Name': {0: 'Ram', 1: 'Deep', 2: 'Yash',
                          3: 'Aman', 4: 'Arjun', 5: 'Aditya',
                          6: 'Akash', 7: 'Chalsea',
                          8: 'Divya'},
                     
                 'Marks': {0: 89, 1: 97, 2: 45,
                           3: 78, 4: 56, 5: 76,
                           6: 81, 7: 87, 8: 100},
                     
                 'Grade': {0: 'B', 1: 'A', 2: 'F',
                           3: 'C', 4: 'E', 5: 'C',
                           6: 'B', 7: 'B', 8: 'A'}})
  
# simple use pivot_table() method
print(pd.pivot_table(df, index = ["ID"]))

Output :

Example 2: Pivot table with multiple columns indexes.

Python3






# import packages
import pandas as pd
  
# create data
df = pd.DataFrame({'ID': {0: 23, 1: 43, 2: 12
                          3: 13, 4: 67, 5: 89,
                          6: 90, 7: 56, 8: 34},
                     
                 'Name': {0: 'Ram', 1: 'Deep', 2: 'Yash',
                          3: 'Aman', 4: 'Arjun', 5: 'Aditya',
                          6: 'Akash', 7: 'Chalsea',
                          8: 'Divya'},
                     
                 'Marks': {0: 89, 1: 97, 2: 45,
                           3: 78, 4: 56, 5: 76,
                           6: 81, 7: 87, 8: 100},
                     
                 'Grade': {0: 'B', 1: 'A', 2: 'F',
                           3: 'C', 4: 'E', 5: 'C',
                           6: 'B', 7: 'B', 8: 'A'}})
# multiple columns with 
# pivot_table() method
display(pd.pivot_table(df, 
                     index = ["ID", "Name"]))

Output :

Example 3: Pivot table with an aggregate function.

Python3




# import packages
import pandas as pd
import numpy as np
  
# create data
df = pd.DataFrame({'ID': {0: 23, 1: 43, 2: 12,
                          3: 13, 4: 67, 5: 89
                          6: 90, 7: 56, 8: 34},
                     
                   'Name': {0: 'Ram', 1: 'Deep',
                            2: 'Yash', 3: 'Aman',
                            4: 'Arjun', 5: 'Aditya',
                            6: 'Akash',7: 'Chalsea',
                            8: 'Divya'},
  
                   'Marks': {0: 89, 1: 97, 2: 45
                             3: 78, 4: 56, 5: 76,
                             6: 81, 7: 87, 8: 100},
  
                   'Grade': {0: 'B', 1: 'A', 2: 'F', 3: 'C',
                             4: 'E', 5: 'C', 6: 'B', 7: 'B',
                             8: 'A'}})
  
# Pivot Table with mean 
# aggregate function on marks
display(pd.pivot_table(df,
                     index = ["Grade"],
                     values = ["Marks"],
                     aggfunc = np.mean))

Output :

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :