Python Pandas: Difference between pivot and pivot_table

In this article, we are going to learn what is the difference between pivot and pivot table in Python pandas?
Submitted by Pranit Sharma, on August 30, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

pandas.DataFrame.pivot() Method

The DataFrame.pivot() is a method in pandas that is used when we need to reorder or reshape the given DataFrame according to index and column values for a better understanding of datasets and to analyze the data according to our compatibility.

It is used for rearranging the DataFrame without applying aggregation. It does not contain duplicate values.

Syntax

DataFrame.pivot(
    index=None, 
    columns=None, 
    values=None)

Example of Python pandas.DataFrame.pivot() Method

# Importing pandas package
import pandas as pd

# Creating dictionary
d = {
    'Fruits':['Apple','Mango','Banana','Apple','Mango','Banana'],
    'Price':[50,80,30,50,80,30],
    'Vitamin':['C','C','B6','C','C','B6']
}

# Creating DataFrame
df = pd.DataFrame(d)

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Pivot the DataFrame
result = df.pivot(index='Fruits', columns='Price', values='Vitamin')

# Display Pivot result
print("Pivot result:\n",result)

The output of the above program is:

Example 1: Difference between pivot and pivot_table

pandas.DataFrame.pivot_table() Method

The DataFrame.pivot_table() method is used to pivot the DataFrame by applying aggregation on it and it allows duplicate values or columns/indexes.

Syntax

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
    )

Example of pandas.DataFrame.pivot_table() Method

# Importing pandas package
import pandas as pd

# Creating a Dictionary
d={
    'A' : ['Amit', 'Amit', 'Ashish', 'Ashish'],
    'B' : ['Bablu', 'Bablu', 'Bobby', 'Bhanu'],
    'C' : ['Chetan', 'Chirag', 'Chiranjeev', 'Chetna']
}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Using pivot table
result = df.pivot_table(values='A', index='B', columns='C', aggfunc=lambda x: len(x.unique()))

# Display result
print("Result:\n",result)

The output of the above program is:

Example 2: Difference between pivot and pivot_table

Difference between pivot() and pivot_table() methods

The pivot() method is a simple method which is used for reshaping the data by specifying the DataFrame's index, columns, and values, whereas the pivot_table() method is a powerful method which is used for creating a pivot table from a DataFrame, it also allows aggregation of the data and can handle duplicate values, whereas pivot() method does not allow it.

The DataFrame.pivot_table() is nothing but a generalization form of DataFrame.pivot(). We can pass a function inside DataFrame.pivot_table() method. DataFrame.Pivot_table() also accepts the list of indices whereas DataFrame.pivot() does not.

Reference(s)

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.